Esempio n. 1
0
    private List<FieldDeclaration> prepareFields() {
      List<FieldDeclaration> fields = Lists.create();

      // shufles
      for (ShuffleModel.Segment segment : shuffle.getSegments()) {
        SimpleName shuffleName = names.create("shuffle");
        shuffleNames.put(segment, shuffleName);
        Name shuffleTypeName = segment.getCompiled().getCombineOutputType().getQualifiedName();
        fields.add(
            factory.newFieldDeclaration(
                null,
                new AttributeBuilder(factory).Private().toAttributes(),
                importer.toType(shuffleTypeName),
                shuffleName,
                null));
      }

      // rendezvous
      for (ReduceUnit unit : reduceUnits) {
        if (unit.canCombine() == false) {
          continue;
        }
        Fragment first = unit.getFragments().get(0);
        SimpleName rendezvousName = names.create("combine");
        rendezvousNames.put(first, rendezvousName);
        fields.add(
            factory.newFieldDeclaration(
                null,
                new AttributeBuilder(factory).Private().toAttributes(),
                importer.toType(first.getCompiled().getQualifiedName()),
                rendezvousName,
                null));
      }
      return fields;
    }
Esempio n. 2
0
    private MethodDeclaration createSetup() {
      Map<FlowElementInput, Segment> segments = Maps.create();
      List<Statement> statements = Lists.create();

      // shuffle outputs
      for (Map.Entry<ShuffleModel.Segment, SimpleName> entry : shuffleNames.entrySet()) {
        ShuffleModel.Segment segment = entry.getKey();
        SimpleName name = entry.getValue();
        Name shuffleTypeName = segment.getCompiled().getCombineOutputType().getQualifiedName();
        statements.add(
            new ExpressionBuilder(factory, factory.newThis())
                .field(name)
                .assignFrom(
                    new TypeBuilder(factory, importer.toType(shuffleTypeName))
                        .newObject(context)
                        .toExpression())
                .toStatement());
        segments.put(segment.getPort(), segment);
      }

      // rendezvous
      for (Map.Entry<Fragment, SimpleName> entry : rendezvousNames.entrySet()) {
        Fragment fragment = entry.getKey();
        Type rendezvousType = importer.toType(fragment.getCompiled().getQualifiedName());
        List<Expression> arguments = Lists.create();
        for (FlowElementInput input : fragment.getInputPorts()) {
          Segment segment = segments.get(input);
          assert segment != null;
          SimpleName shuffleName = shuffleNames.get(segment);
          assert shuffleName != null;
          arguments.add(
              new ExpressionBuilder(factory, factory.newThis()).field(shuffleName).toExpression());
        }
        SimpleName name = entry.getValue();
        statements.add(
            new ExpressionBuilder(factory, factory.newThis())
                .field(name)
                .assignFrom(
                    new TypeBuilder(factory, rendezvousType).newObject(arguments).toExpression())
                .toStatement());
      }

      return factory.newMethodDeclaration(
          null,
          new AttributeBuilder(factory).annotation(t(Override.class)).Public().toAttributes(),
          Collections.<TypeParameterDeclaration>emptyList(),
          t(void.class),
          factory.newSimpleName("setup"),
          Collections.singletonList(
              factory.newFormalParameterDeclaration(
                  factory.newNamedType(factory.newSimpleName("Context")), context)),
          0,
          Arrays.asList(t(IOException.class), t(InterruptedException.class)),
          factory.newBlock(statements));
    }
 private void compile(Fragment fragment, StageModel stage) throws IOException {
   if (fragment.isRendezvous()) {
     CompiledType compiled =
         new ReduceFragmentEmitter(environment)
             .emit(fragment, stage.getShuffleModel(), stage.getStageBlock());
     fragment.setCompiled(compiled);
   } else {
     CompiledType compiled =
         new MapFragmentEmitter(environment).emit(fragment, stage.getStageBlock());
     fragment.setCompiled(compiled);
   }
 }
  /** グループごとの最小値を求めるテスト。 */
  @Test
  public void withParameter() {
    List<StageModel> stages = compile(GroupSortFlowWithParameter.class);
    StageModel stage = stages.get(0);
    Assume.assumeThat(stage.getReduceUnits().size(), is(1));
    ReduceUnit reduce = stage.getReduceUnits().get(0);
    Fragment fragment = reduce.getFragments().get(0);
    Name name = fragment.getCompiled().getQualifiedName();

    ClassLoader loader = start();
    PortMapper mapper = new PortMapper(fragment);
    MockResult<Ex1> r1 = mapper.add("r1", new Ex1Copier());
    MockResult<Ex1> r2 = mapper.add("r2", new Ex1Copier());

    @SuppressWarnings("unchecked")
    Rendezvous<Writable> f = (Rendezvous<Writable>) create(loader, name, mapper.toArguments());

    Segment segment = stage.getShuffleModel().findSegment(fragment.getInputPorts().get(0));
    SegmentedWritable value = createShuffleValue(loader, stage);

    Ex1 ex1 = new Ex1();
    f.begin();

    ex1.setValue(50);
    setShuffleValue(segment, value, ex1);
    f.process(value);

    ex1.setValue(100);
    setShuffleValue(segment, value, ex1);
    f.process(value);

    ex1.setValue(101);
    setShuffleValue(segment, value, ex1);
    f.process(value);

    ex1.setValue(150);
    setShuffleValue(segment, value, ex1);
    f.process(value);

    f.end();

    assertThat(r1.getResults().size(), is(2));
    assertThat(r2.getResults().size(), is(2));
    assertThat(r1.getResults().get(0).getValue(), is(50));
    assertThat(r1.getResults().get(1).getValue(), is(100));
    assertThat(r2.getResults().get(0).getValue(), is(101));
    assertThat(r2.getResults().get(1).getValue(), is(150));
  }
  /** 単純なテスト。 */
  @Test
  public void simple() {
    List<StageModel> stages = compile(BranchFlowSimple.class);
    Fragment fragment = stages.get(0).getMapUnits().get(0).getFragments().get(0);
    Name name = fragment.getCompiled().getQualifiedName();

    ClassLoader loader = start();
    PortMapper mapper = new PortMapper(fragment);
    MockResult<Ex1> high = mapper.create("high");
    MockResult<Ex1> low = mapper.create("low");
    MockResult<Ex1> stop = mapper.create("stop");

    @SuppressWarnings("unchecked")
    Result<Ex1> f = (Result<Ex1>) create(loader, name, mapper.toArguments());

    Ex1 ex1 = new Ex1();
    ex1.setValue(-100);
    f.add(ex1);
    assertThat(high.getResults().size(), is(0));
    assertThat(low.getResults().size(), is(0));
    assertThat(stop.getResults().size(), is(1));
    assertThat(stop.getResults().get(0), is(ex1));
    stop.getResults().clear();

    ex1.setValue(0);
    f.add(ex1);
    assertThat(high.getResults().size(), is(0));
    assertThat(low.getResults().size(), is(0));
    assertThat(stop.getResults().size(), is(1));
    assertThat(stop.getResults().get(0), is(ex1));
    stop.getResults().clear();

    ex1.setValue(1);
    f.add(ex1);
    assertThat(high.getResults().size(), is(0));
    assertThat(low.getResults().size(), is(1));
    assertThat(stop.getResults().size(), is(0));
    assertThat(low.getResults().get(0), is(ex1));
    low.getResults().clear();

    ex1.setValue(50);
    f.add(ex1);
    assertThat(high.getResults().size(), is(0));
    assertThat(low.getResults().size(), is(1));
    assertThat(stop.getResults().size(), is(0));
    assertThat(low.getResults().get(0), is(ex1));
    low.getResults().clear();

    ex1.setValue(100);
    f.add(ex1);
    assertThat(high.getResults().size(), is(0));
    assertThat(low.getResults().size(), is(1));
    assertThat(stop.getResults().size(), is(0));
    assertThat(low.getResults().get(0), is(ex1));
    low.getResults().clear();

    ex1.setValue(101);
    f.add(ex1);
    assertThat(high.getResults().size(), is(1));
    assertThat(low.getResults().size(), is(0));
    assertThat(stop.getResults().size(), is(0));
    assertThat(high.getResults().get(0), is(ex1));
    high.getResults().clear();

    ex1.setValue(150);
    f.add(ex1);
    assertThat(high.getResults().size(), is(1));
    assertThat(low.getResults().size(), is(0));
    assertThat(stop.getResults().size(), is(0));
    assertThat(high.getResults().get(0), is(ex1));
    high.getResults().clear();
  }