示例#1
0
文件: star.c 项目: jsyk/snet-rts
static void TerminateStarBoxTask(snet_stream_desc_t *outstream,
                                 star_arg_t *sarg)
{
  /* close streams */
  SNetStreamClose( outstream, false);

  /* destroy the task argument */
  SNetExprListDestroy( sarg->guards);
  SNetLocvecDestroy(SNetLocvecGet(sarg->info));
  SNetInfoDestroy(sarg->info);
  SNetVariantListDestroy( sarg->exit_patterns);
  SNetMemFree( sarg);
}
示例#2
0
/**
 * Convenience function for creating
 * Split, DetSplit, LocSplit or LocSplitDet,
 * dependent on parameters is_byloc and is_det
 */
snet_stream_t *CreateSplit( snet_stream_t *input,
    snet_info_t *info,
    snet_locvec_t *locvec,
    int location,
    snet_ast_t *box_a,
    int ltag, int utag,
    bool is_byloc,
    bool is_det
    )
{
  snet_info_t *newInfo = SNetInfoCopy(info);
  snet_stream_t *initial, *output;
  split_arg_t *sarg;
  snet_locvec_t *locvec;

  locvec = SNetLocvecGet(info);
  SNetLocvecSplitEnter(locvec);
  SNetLocvecSet(newInfo, SNetLocvecCopy(locvec));

  input = SNetRouteUpdate(newInfo, input, location);
  if(SNetDistribIsNodeLocation(location)) {
    initial = SNetStreamCreate(0);

    sarg = (split_arg_t *) SNetMemAlloc( sizeof( split_arg_t));
    sarg->input  = input;
    sarg->output = initial;
    sarg->boxfun = box_a;
    sarg->info = newInfo;
    sarg->ltag = ltag;
    sarg->utag = utag;
    sarg->is_det = is_det;
    sarg->is_byloc = is_byloc;
    sarg->location = location;
    SNetThreadingSpawn(
        SNetEntityCreate( ENTITY_split, location, locvec,
          "<split>", SplitBoxTask, (void*)sarg)
        );

    output = CollectorCreateDynamic( initial, location, info);

  } else {
    SNetLocvecDestroy(SNetLocvecGet(newInfo));
    SNetInfoDestroy(newInfo);
    output = input;
  }
  SNetLocvecSplitLeave(locvec);

  return output;
}
示例#3
0
void SNetRouteNewDynamic(snet_dest_t dest)
{
  snet_ast_t *ast = NULL; //FIXME
  snet_stream_t *stream;

  snet_info_t *info = SNetInfoInit();
  SNetInfoSetTag(info, prevDest, (uintptr_t) SNetDestCopy(&dest),
                (void* (*)(void*)) &SNetDestCopy, &SNetMemFree);

  SNetRouteDynamicEnter(info, dest.dynamicIndex, dest.dynamicLoc, NULL);
  stream = SNetInstantiate(ast, NULL, info);
  SNetRouteUpdate(info, stream, dest.parentNode);
  SNetRouteDynamicExit(info, dest.dynamicIndex, dest.dynamicLoc, NULL);

  SNetInfoDestroy(info);
}
示例#4
0
void SNetRouteNewDynamic(snet_dest_t dest)
{
  snet_startup_fun_t fun = SNetIdToNet(dest.parent);

  snet_info_t *info = SNetInfoInit();
  SNetInfoSetTag(info, prevDest, (uintptr_t) SNetDestCopy(&dest),
                (void* (*)(void*)) &SNetDestCopy);

  SNetLocvecSet(info, SNetLocvecCreate());

  SNetRouteDynamicEnter(info, dest.dynamicIndex, dest.dynamicLoc, NULL);

  //TODO may not need to update loc here

  SNetRouteUpdate(info, fun(NULL, info, dest.dynamicLoc), dest.parentNode);
  SNetRouteDynamicExit(info, dest.dynamicIndex, dest.dynamicLoc, NULL);

  SNetInfoDestroy(info);
}
示例#5
0
/**
 * Split box task.
 *
 * Implements both the non-deterministic and deterministic variants.
 */
static void SplitBoxTask(snet_entity_t *ent, void *arg)
{
  int i;
  split_arg_t *sarg = (split_arg_t *)arg;
  snet_stream_desc_t *initial, *instream;
  int ltag_val, utag_val;
  snet_info_t *info;
  snet_record_t *rec;
  snet_locvec_t *locvec;
  bool terminate = false;
  /* a list of all outstreams for all yet created instances */
  snet_streamset_t repos_set = NULL;
  snet_stream_iter_t *iter = SNetStreamIterCreate( &repos_set);
  /* a hashtable for fast lookup, initial capacity = 2^4 = 16 */
  hashtab_t *repos_tab = HashtabCreate( 4);
  (void) ent; /* NOT USED */

  /* for deterministic variant: */
  int counter = 0;

  initial = SNetStreamOpen(sarg->output, 'w');
  instream = SNetStreamOpen(sarg->input, 'r');

  /* MAIN LOOP START */
  while( !terminate) {
    /* read from input stream */
    rec = SNetStreamRead( instream);

    switch( SNetRecGetDescriptor( rec)) {

      case REC_data:
        /* get lower and upper tag values */
        ltag_val = SNetRecGetTag( rec, sarg->ltag);
        utag_val = SNetRecGetTag( rec, sarg->utag);

        /* for all tag values */
        for( i = ltag_val; i <= utag_val; i++) {
          snet_stream_desc_t *outstream = HashtabGet( repos_tab, i);

          if( outstream == NULL) {
            snet_stream_t *temp_stream;
            snet_stream_t *newstream_addr = SNetStreamCreate(0);
            /* instance does not exist yet, create it */
            outstream = SNetStreamOpen(newstream_addr, 'w');
            /* add to lookup table */
            HashtabPut( repos_tab, i, outstream);
            /* add to list */
            SNetStreamsetPut( &repos_set, outstream);

            /* create info and location vector for creation of this replica */
            info = SNetInfoCopy(sarg->info);
            locvec = SNetLocvecSplitSpawn(SNetLocvecGet(sarg->info), i);
            SNetLocvecSet(info, locvec);

            if( sarg->is_byloc) {
              SNetRouteDynamicEnter(info, i, i, sarg->boxfun);
              temp_stream = sarg->boxfun(newstream_addr, info, i);
              temp_stream = SNetRouteUpdate(info, temp_stream, sarg->location);
              SNetRouteDynamicExit(info, i, i, sarg->boxfun);
            } else {
              SNetRouteDynamicEnter(info, i, sarg->location, sarg->boxfun);
              temp_stream = sarg->boxfun(newstream_addr, info, sarg->location);
              temp_stream = SNetRouteUpdate(info, temp_stream, sarg->location);
              SNetRouteDynamicExit(info, i, sarg->location, sarg->boxfun);
            }

            /* destroy info and location vector */
            SNetLocvecDestroy(locvec);
            SNetInfoDestroy(info);

            if(temp_stream != NULL) {
              /* notify collector about the new instance via initial */
              SNetStreamWrite( initial,
                  SNetRecCreate( REC_collect, temp_stream));
            }
          } /* end if (outstream==NULL) */

          /* multicast the record */
          SNetStreamWrite( outstream,
              /* copy record for all but the last tag value */
              (i!=utag_val) ? SNetRecCopy( rec) : rec
              );
        } /* end for all tags  ltag_val <= i <= utag_val */

        /* If deterministic, append a sort record to *all* registered
         * instances and the initial stream.
         */
        if( sarg->is_det ) {
          /* reset iterator */
          SNetStreamIterReset( iter, &repos_set);
          while( SNetStreamIterHasNext( iter)) {
            snet_stream_desc_t *cur_stream = SNetStreamIterNext( iter);

            SNetStreamWrite( cur_stream,
                SNetRecCreate( REC_sort_end, 0, counter));
          }
          /* Now also send a sort record to initial,
             after the collect records for new instances have been sent */
          SNetStreamWrite( initial,
              SNetRecCreate( REC_sort_end, 0, counter));
        }
        /* increment counter for deterministic variant */
        counter += 1;
        break;

      case REC_sync:
        {
          snet_stream_t *newstream = SNetRecGetStream( rec);
          SNetStreamReplace( instream, newstream);
          SNetRecDestroy( rec);
        }
        break;

      case REC_sort_end:
        /* broadcast the sort record */
        SNetStreamIterReset( iter, &repos_set);
        /* all instances receive copies of the record */
        while( SNetStreamIterHasNext( iter)) {
          snet_stream_desc_t *cur_stream = SNetStreamIterNext( iter);
          SNetStreamWrite( cur_stream,
              SNetRecCreate( REC_sort_end,
                /* we have to increase level */
                SNetRecGetLevel( rec)+1,
                SNetRecGetNum( rec))
              );
        }
        /* send the original record to the initial stream,
           but with increased level */
        SNetRecSetLevel( rec, SNetRecGetLevel( rec) + 1);
        SNetStreamWrite( initial, rec);
        break;

      case REC_terminate:

        SNetStreamIterReset( iter, &repos_set);
        /* all instances receive copies of the record */
        while( SNetStreamIterHasNext( iter)) {
          snet_stream_desc_t *cur_stream = SNetStreamIterNext( iter);
          SNetStreamWrite( cur_stream, SNetRecCopy( rec));

          SNetStreamIterRemove( iter);
          /* close  the stream to the instance */
          SNetStreamClose( cur_stream, false);
        }
        /* send the original record to the initial stream */
        SNetStreamWrite( initial, rec);
        /* note that no sort record has to be appended */
        terminate = true;
        break;

      case REC_collect:
        /* invalid control record */
      default:
        assert( 0);
        /* if ignore, at least destroy it */
        SNetRecDestroy( rec);
    }
  } /* MAIN LOOP END */

  /* destroy repository */
  HashtabDestroy( repos_tab);
  SNetStreamIterDestroy( iter);

  /* close and destroy initial stream */
  SNetStreamClose( initial, false);
  /* close instream */
  SNetStreamClose( instream, true);

  SNetLocvecDestroy(SNetLocvecGet(sarg->info));
  SNetInfoDestroy(sarg->info);
  /* destroy the argument */
  SNetMemFree( sarg);
} /* END of SPLIT BOX TASK */
示例#6
0
/**
 * Main starting entry point of the SNet program
 */
int SNetInRun(int argc, char **argv,
              char *static_labels[], int number_of_labels,
              char *static_interfaces[], int number_of_interfaces,
              snet_startup_fun_t fun)
{
  FILE *input = stdin;
  FILE *output = stdout;
  snet_stream_t *input_stream = NULL;
  snet_stream_t *output_stream = NULL;
  int i = 0;
  snet_info_t *info = NULL;
  snet_locvec_t *locvec;
  snetin_label_t *labels = NULL;
  snetin_interface_t *interfaces = NULL;
  char *brk;
  char addr[256];
  int len;
  int port;
  
  /* Parse argv: */
  for(i = 1; i < argc; i++) {
    if(strcmp(argv[i], "-h") == 0) {
      /* Help */
      SNetRuntimeHelpText();

      if(input != stdin && input != NULL) {
	SNetInClose(input);
      }

      if(output != stdout && output != NULL) {
	SNetInClose(output);
      }

      return 0;

    } else if(strcmp(argv[i], "-i") == 0 && input == stdin && i + 1 <= argc) {
      /* Input from file */
      i = i + 1;
      input =  SNetInOpenFile(argv[i], "r");
    } else if(strcmp(argv[i], "-I") == 0 && input == stdin && i + 1 <= argc) {
      /* Input from socket */
      i = i + 1;
      input = SNetInOpenInputSocket(atoi(argv[i]));
    } else if(strcmp(argv[i], "-o") == 0 && output == stdout && i + 1 <= argc) {
      /* Output to file */
      i = i + 1;
      output = SNetInOpenFile(argv[i], "w");
    } else if(strcmp(argv[i], "-O") == 0 && output == stdout && i + 1 <= argc) {
      /* Output to socket */
      i = i + 1;
      brk = strchr(argv[i], ':');

      if(brk == NULL) {
	output = NULL;

	SNetUtilDebugFatal("Could not parse URL!\n");
      }

      len = brk - argv[i];
      strncpy((char *)addr, (const char *)argv[i], len);
      addr[len] = '\0';
      port = atoi(brk + 1);

      output = SNetInOpenOutputSocket(addr, port);
    }
  }

  if(input == NULL) {

    if(output != stdout && output != NULL) {
      SNetInClose(output);
    }

    SNetUtilDebugFatal("");
  }

  if(output == NULL) {

    if(input != stdin && input != NULL) {
      SNetInClose(input);
    }

    SNetUtilDebugFatal("");
  }


  /* Actual SNet network interface main: */

  /* check for number of interfaces */
  if (0 == number_of_interfaces) {
    SNetUtilDebugNotice("No language interfaces were specified by the source program!");
    exit(1);
  }
  
  labels     = SNetInLabelInit(static_labels, number_of_labels);
  interfaces = SNetInInterfaceInit(static_interfaces, number_of_interfaces);

  info = SNetInfoInit();


  SNetDistribInit(argc, argv, info);

  (void) SNetThreadingInit(argc, argv);

  //SNetObserverInit(labels, interfaces); 

  locvec = SNetLocvecCreate();
  SNetLocvecSet(info, locvec);

  SNetDistribStart();

  if (SNetDistribIsRootNode()) {
    input_stream = SNetStreamCreate(0);
    output_stream = fun(input_stream, info, 0);
    output_stream = SNetRouteUpdate(info, output_stream, 0);
    
    /* create output thread */
    SNetInOutputInit(output, labels, interfaces, output_stream);

    /* create input thread */
    SNetInInputInit(input, labels, interfaces, input_stream); 
    
    SNetRuntimeStartWait(input_stream, info, output_stream);  
    
    /* tell the threading layer that it is ok to shutdown,
     and wait until it has stopped such that it can be cleaned up */
    (void) SNetThreadingStop();
  }

  (void) SNetThreadingCleanup();
  SNetInfoDestroy(info);

  SNetLocvecDestroy(locvec);

  /* destroy observers */
  //SNetObserverDestroy();

  SNetInLabelDestroy(labels);
  SNetInInterfaceDestroy(interfaces);

  if(input != stdin) {
    SNetInClose(input);
  }

  if(output != stdout) {
    SNetInClose(output);
  }

  return 0;
}
示例#7
0
/**
 * Main starting entry point of the SNet program
 */
int SNetInRun(int argc, char **argv,
              char *static_labels[], int number_of_labels,
              char *static_interfaces[], int number_of_interfaces,
              snet_startup_fun_t fun)
{
  FILE *input = NULL;
  FILE *output = NULL;
  snet_stream_t *input_stream = NULL;
  snet_stream_t *output_stream = NULL;
  snet_info_t *info;
  snet_locvec_t *locvec;
  snetin_label_t *labels = NULL;
  snetin_interface_t *interfaces = NULL;

  if (0 == SNetInParseOptions(argc, argv, &input, &output)) {
    return 0;
  }

  /* Actual SNet network interface main: */

  /* check for number of interfaces */
  if (0 == number_of_interfaces) {
    SNetUtilDebugNotice("No language interfaces were specified by the source program!");
    exit(1);
  }

  labels     = SNetInLabelInit(static_labels, number_of_labels);
  interfaces = SNetInInterfaceInit(static_interfaces, number_of_interfaces);

  info = SNetInfoInit();

  SNetDistribInit(argc, argv, info);

  (void) SNetThreadingInit(argc, argv);

  SNetObserverInit(labels, interfaces);

  locvec = SNetLocvecCreate();
  SNetLocvecSet(info, locvec);

  input_stream = SNetStreamCreate(0);
  output_stream = fun(input_stream, info, 0);
  output_stream = SNetRouteUpdate(info, output_stream, 0);

  SNetDistribStart();

  if (SNetDistribIsRootNode()) {
    /* create output thread */
    SNetInOutputInit(output, labels, interfaces, output_stream);

    /* create input thread */
    SNetInInputInit(input, labels, interfaces, input_stream);
  }

  SNetRuntimeStartWait(input_stream, info, output_stream);

  /* tell the threading layer that it is ok to shutdown,
     and wait until it has stopped such that it can be cleaned up */
  (void) SNetThreadingStop();

  (void) SNetThreadingCleanup();

  SNetInfoDestroy(info);

  SNetLocvecDestroy(locvec);

  /* destroy observers */
  SNetObserverDestroy();

  SNetInLabelDestroy(labels);
  SNetInInterfaceDestroy(interfaces);

  if(input != stdin) {
    SNetInClose(input);
  }

  if(output != stdout) {
    SNetInClose(output);
  }

  return 0;
}