Beispiel #1
0
int reduce(int* arr, int n, int(*reducer)(int, int)) {
	int tmp = reducer(arr[0], arr[1]);
	for(int i = 2; i < n; i++) {
		tmp = reducer(tmp, arr[i]);
	}
	return tmp;
}
Beispiel #2
0
void * __list_reduce(list_t *list, reduce_t reducer, void *data, reduce_type_t type) {
  free_t      f;
  listnode_t *node;
  void       *elem;

  f = (type == RTStrs) ? (free_t) data_free : NULL;
  node = list_head_pointer(list);
  while (node && _ln_datanode(node)) {
    elem = node -> data;
    switch (type) {
      case RTChars:
        elem =  list -> type.tostring(elem);
        break;
      case RTStrs:
        elem =  str_wrap(list -> type.tostring(elem));
        break;
      default:
        break;
    }
    data = reducer(elem, data);
    if (f) {
      f(elem);
    }
    node = node -> next;
  }
  return data;
}
void *reduce(ArrayUtil util, ReducerFunc * reducer, void * hint, void * intialValue){
  void *base = util.base;
  void *previousvalue = intialValue;
  for (size_t i = 0; i <util.length ; i++) {
    previousvalue = reducer(hint,previousvalue,base+(util.typeSize*i));
  }
  return previousvalue;
};
Beispiel #4
0
void* reduce(ArrayUtil util, ReducerFunc* reducer, void* hint, void* initialValue){
	void *base = util.base;
    for (int i = 0; i < util.length; i++) {
    	initialValue  = reducer(hint ,initialValue ,base);
    	base = base+util.typeSize;
    }
	return initialValue;
};
Beispiel #5
0
void* reduce(ArrayUtil util, ReducerFunc* reducer, void* hint, void* intialValue){
	void *item;
	for (int i = 0; i < util.length; i++){
		item = util.base+(i*util.typeSize);
		intialValue = reducer(hint,intialValue,item);
	};
	return item;
};
Beispiel #6
0
void * reduce(Array_util array, Reducer_function * reducer, void * hint, void * intial_value) {
    void * elements = array.base;
    for (int i = 0; i < array.length; i++) {
        intial_value = reducer(hint, intial_value, elements);
        elements += array.type_size;
    }
    return intial_value;
};
Beispiel #7
0
inline std::uint16_t
_ccs(int &x, const V1 &v1, const V2 &v2, const T &... tail) noexcept
{
    // NOTE: Below line should not be merged into single return statement. Due
    //       to evaluation order of function arguments is unspecified behaviour
    //       and there is no warranty of /Sequenced Before/ for modifiable
    //       variable `x`.
    const auto a = _ccs(x, v1);
    return reducer(a, _ccs(x, v2, tail...));
}
 T& VirtualSudokuBoard::iterateOverBoard(T& intialVal, std::function<T&(T&, int, int, int)>& reducer) const
 {
     T& result = intialVal;
     for(int row = 1; row <= this->boardSize; ++row) {
         for(int column = 1; column <= this->boardSize; ++column) {
             result = reducer(result, row, column, this->getEntry(row, column));
         }
     }
     return result;
 }
Beispiel #9
0
void *reduce(ArrayUtil util, ReducerFunc * reducer, void *hint, void *initialValue){
  void *item = util.base;
  void *previousItem = initialValue;
  for(int i = 0; i < util.length; i++){
    void *ptr = reducer(hint, previousItem, item);
    previousItem = ptr;
    item = item + util.typeSize;
  };
  return previousItem;
};
Beispiel #10
0
int reducer( int input ) {

	if( input < 10 ) {
		return input;
	}

	else {
		return( input % 10 ) * reducer( input / 10 );
	}

}
Beispiel #11
0
void *reduce(ArrayUtil arr, ReducerFunc* reducer, void* hint, void* intialValue){
  void *base = arr.base;
  void *reduced = (void *)calloc(1, arr.length);
	memcpy(reduced, intialValue, arr.length);

	for (int i = 0; i < arr.length; ++i){
		reducer(hint, reduced, base);
		base += arr.type_size;
	}
	return reduced;
};
Beispiel #12
0
void* reduce(ArrayUtil array, ReducerFunc* reducer, void* hint, void* initialValue){
    void *previousItem = initialValue;
    void *currentItem = array.base;
    void *end = array.base + (array.length * array.typeSize);

    for( ; currentItem != end; ){
        previousItem = reducer(hint, previousItem, currentItem);
        currentItem += array.typeSize;
    }
    return previousItem;
}
 T& VirtualSudokuBoard::iterateOverColumn(int column, T& intialVal, std::function<T&(T&, int, int, int)>& reducer) const throw(OpenSudoku::InvalidDimension)
 {
     if (NOT(VAL_IN_RANGE(column, 1, this->boardSize))) {
         throw InvalidDimension(column);
     }
     T& result = intialVal;
     for(int row = 1; row <= this->boardSize; ++row) {
         result = reducer(result, row, column, this->getEntry(row, column));
     }
     return result;
 }
int main(int argc, char const *argv[]) {

    double time;
    time=-omp_get_wtime();

    if (argc==1) {
        printf("Error! There is no input file!\n");
        return 1;
    }

    int i;
    int file_num=argc-1; //the number of files

    struct wordTuple* mapWorkQueue[file_num];

    #pragma omp parallel for
    for(i=0; i<file_num; ++i) {
        reader(argv[i+1], &mapWorkQueue[i]);
    }

    struct wordTuple* mapResult[MAXUNIQUEWORD];

    int reduceCount=0;
    char* reduceWord[MAXUNIQUEWORD];
    int* reduceWorkQueue[MAXUNIQUEWORD];

    #pragma omp parallel for
    for(i=0; i<file_num; i++) {
        mapper(&mapResult[i],mapWorkQueue[i]);
        struct wordTuple *temp, *cur = mapResult[i];
        while(cur!=NULL) {
            #pragma omp critical
            hash(reduceWorkQueue, reduceWord, cur->key, cur->data, &reduceCount, i, file_num);
            temp=cur;
            cur=cur->next;
            freeWordTuple(temp);
        }
    }

    struct wordTuple *reduceResult[MAXUNIQUEWORD];

    #pragma omp parallel for
    for (i = 0; i<reduceCount; ++i) {
        reducer(reduceWorkQueue[i], reduceWord[i], &reduceResult[i], file_num);
        free(reduceWorkQueue[i]);
    }

    writer(reduceResult, reduceCount);

    time+=omp_get_wtime();
    printf("Time = %f\n",time);
    return EXIT_SUCCESS;
}
Beispiel #15
0
int persistence( int input ) {

	if( input < 10 ) {
		return 0;
	}

	else {
		int digit = reducer( input );
		return 1 + persistence( digit );
	}

}
Beispiel #16
0
void *reduce(LinkedList list, Reducer reducer, void *hint, void *initialValue) {
	Element *element = list.head;
	void **tempDest = malloc(SIZE_OF_ADDRESS);
	if(initialValue != NULL){
		*tempDest = initialValue;
	}
	else{
		*tempDest = element->value;
		element = element->next;
	}
	while(element != NULL){		
		*tempDest = reducer(hint, *tempDest, element->value);
		element = element->next;
	}
	return *tempDest;
}
Beispiel #17
0
/*
 * computes a rainbow chain: iteratively reduces a digest and projects it back 
 * into the password space; goes from `from` up to `upper_bound` (exclusive)
 */
static int rainbow_reduce_chain(const config_t * config, const rule_info_t * rule,
								int from, int upper_bound, unsigned char * digest,
								char * password, int max_password)
{
	int i;
	uint64_t k;

	for (i = from; i < upper_bound; i++) {
		k = reducer(rule, config->seed_table[i], digest);
		if (rule_kth_password(rule, k, password, max_password, 0) != RULE_STATUS_OK) {
			/* error message printed by rule_kth_password */
			return RAINBOW_STATUS_ERROR;
		}
		config->hash_func((unsigned char*)password, strlen(password), digest);
	}
	return RAINBOW_STATUS_OK;
}
Beispiel #18
0
TEST(ReducerTest, AcceptsSubmissions)
{
    bool success = false;
    mr::ReduceFunction reduceFunction = [&success]( mr::bytelist key
                                                  , std::vector<mr::bytelist> values
                                                  , mr::OutputCollector* collector
                                                  )
    {
        success = true;
    };

    mr::Reducer reducer(reduceFunction, 0);
    mr::bytelist key {'a','b','c','d'};
    std::vector<mr::bytelist> values { {'a'}, {'b','c'}, {'d','e','f'} };
    reducer.submit(key, values);

    ASSERT_TRUE(success);
}
void UmlReduceAction::write(FileOut & out) {
  write_begin(out, "ReduceAction");
  
  if (isOrdered())
    out << " isOrdered=\"true\"";
  
  write_end(out, TRUE);

  UmlItem * r = reducer();
  
  if (r != 0) {
    out.indent();
    out << "<reducer";
    out.idref(r);
    out << "/>\n";
  }

  write_close(out);
}
 T& VirtualSudokuBoard::iterateOverSquare(int square, T& intialVal, std::function<T&(T&, int, int, int)>& reducer) const throw(OpenSudoku::InvalidDimension)
 {
     if (this->boardSize != NINE) {
         throw SudokuException("iterateOverSquare makes sense for board with size 9");
     }
     
     if (NOT(VAL_IN_RANGE(square, 1, this->boardSize))) {
         throw InvalidDimension(square);
     }
     T& result = intialVal;
     int s_row = ((int) (square - 1) / 3) * 3, s_col = ((square - 1) % 3) * 3;
     for (int i = 1; i <= 3; i++) {
         for (int j = 1; j <= 3; j++) {
             int row = i + s_row, column =j + s_col;
             result = reducer(result, row, column, this->getEntry(row, column));
         }
     }
     return result;
 }
void LogMapper::send_bucket_partition_buffer(
  const Bucket* bucket,
  storage::PartitionId partition,
  const char* send_buffer,
  uint32_t log_count,
  uint64_t written,
  uint32_t shortest_key_length,
  uint32_t longest_key_length) {
  if (written == 0) {
    return;
  }

  LogReducerRef reducer(engine_, partition);
  reducer.append_log_chunk(
    bucket->storage_id_,
    send_buffer,
    log_count,
    written,
    shortest_key_length,
    longest_key_length);
}
Beispiel #22
0
void draw_horiz_specgram()
{
	active_drawing_area = height-(2*border);
	gdk_threads_enter();
	if (display_markers)
	{
                update_freq_markers();
                clear_display = FALSE;
                display_markers = FALSE;
	}
	gdk_window_copy_area(main_pixmap,gc,
			0,border,
			main_pixmap,
			tape_scroll,border,
			width-horiz_spec_start,
			active_drawing_area);

	reducer(low_freq, high_freq, active_drawing_area);

	for (i=0; i < active_drawing_area; i++)
	{
		lvl=(gint)pip_arr[i]*4;
		if (lvl > (MAXBANDS-1))
			lvl=(MAXBANDS-1);
		gdk_gc_set_foreground(gc,&colortab[16][lvl]);

		gdk_draw_line(main_pixmap,gc,
				width-horiz_spec_start-tape_scroll, 
				active_drawing_area-i+border,
				width-horiz_spec_start,
				active_drawing_area-i+border);
	}

	gdk_window_clear(main_display->window);

	gdk_threads_leave();
}
Beispiel #23
0
int main(int argc, char **argv) {

  int i;
  /*** COMMAND LINE ARG PARSING BEGIN ***/

  // default values
  char app = WORDCOUNT;
  impl = THREADS;
  int num_maps = -1;
  int num_reduces = -1;
  char *infile = "input/wordcount.input";
  char *outfile = "output/wordcount.output";

  // parsing
  int c;
  static char usage[] = "usage: %s --app [wordcount, sort] --impl [procs, threads] --maps num_maps --reduces num_reduces --input infline --output outfile\n";
  while (1) {
    static struct option long_options[] =
    {
      {"app",      required_argument, 0, 'a'}, // [wordcount, sort]
      {"impl",     required_argument, 0, 'b'}, // [procs, threads]
      {"maps",     required_argument, 0, 'c'}, // num_maps
      {"reduces",  required_argument, 0, 'd'}, // num_reduces
      {"input",    required_argument, 0, 'e'}, // infile
      {"output",   required_argument, 0, 'f'}, // outfile
      {0, 0, 0, 0}
    };
    int option_index = 0;
    c = getopt_long (argc, argv, "a:b:c:d:e:f",
                     long_options, &option_index);

    // check for end of arguments
    if (c == -1)
      break;
    // set & configure
    switch (c) {
      case 'a': // --app
        if (strcmp(optarg, "wordcount") == 0 )
          app = WORDCOUNT;
        else if (strcmp(optarg, "sort") == 0)
          app = SORT;
        // additional apps go here
        else {
          fprintf(stderr, usage, argv[0]);
          exit(1);
        }
        break;
      case 'b': // --impl
        if (strcmp(optarg, "procs") == 0 )
          impl = PROCS;
        else if (strcmp(optarg, "threads") == 0)
          impl = THREADS;
        else {
          fprintf(stderr, usage, argv[0]);
          exit(1);
        }
        break;
      case 'c': // --maps
        num_maps = atoi(optarg);
        break;
      case 'd': // --reduces
        num_reduces = atoi(optarg);
        break;
      case 'e': // --infile
        infile = optarg;
        break;
      case 'f': // --outfile
        outfile = optarg;
        break;
      case '?':
        break;
      default:
        abort();
    }
  }
  /*** COMMAND LINE ARG PARSING END ***/

#ifdef DEBUG
  printf("\n[DEBUG ENABLED]\n\n");
  // print options
  printf("##################\n");
  printf("# MAPRED OPTIONS #\n");
  printf("##################\n");
  if (app == WORDCOUNT)
    printf("# app:      WORDCOUNT  \n", app);
  else if (app == SORT)
    printf("# app:      SORT  \n", app);
  if (impl == PROCS)
    printf("# impl:     PROCS  \n", impl);
  else if (impl == THREADS)
    printf("# impl:     THREADS  \n", impl);
  printf("# maps:     %d  \n", num_maps);
  printf("# reduces:  %d  \n", num_reduces);
  printf("# infile:   %s  \n", infile);
  printf("# outfile:  %s  \n", outfile);
#endif

  spec_t *spec = create_spec();
  if (num_maps > 0)
    spec->maps = num_maps;
  else
    spec->maps = DEFAULT_MAPS;
  if (num_reduces > 0)
    spec->reduces = num_reduces;
  else
    spec->reduces = DEFAULT_REDUCES;

  // function pointers for read, write, compare, map, and reduce
  void (*map)(void*, void*, spec_t*);
  int (*compare)(void*, void*);
  void (*reduce)(void*, void*, spec_t*, int, int);

  // get app-specific functions for read, write, compare, map, and reduce
  switch (app) {
    case WORDCOUNT:
      map = &wc_map;
      reduce = &wc_reduce;
      compare = &wc_compare;
      spec->work = MAP_REDUCE;
      break;
    case SORT:
      map = &is_map;
      //reduce = &is_reduce;
      compare = &is_compare;
      spec->work = MAP_SORT;
      break;
  }
#ifdef TIME
  clock_t start, end;
#endif

  // READ
#ifdef DEBUG
  printf("\n##############\n");
  printf("# READ START #\n");
  printf("##############\n");
#endif
#ifdef TIME
  start = clock();
#endif
  reader(spec, infile);
#ifdef TIME
  end = clock();
  printf("READ: %.2fms\n", (double)(end-start)/CLOCKS_PER_SEC*1000);
#endif
#ifdef WCDEBUG
  wc_debug(spec, INPUT);
#endif

  // MAP
#ifdef DEBUG
  printf("\n################\n");
  printf("# MAPPER START #\n");
  printf("################\n");
#endif
#ifdef TIME
  start = clock();
#endif
  for (i = 0; i < spec->inputRecords; i += spec->maps)
    mapper(spec, map, i);
#ifdef TIME
  end = clock();
  printf("MAP: %.2fms\n", (double)(end-start)/CLOCKS_PER_SEC*1000);
#endif
#ifdef WCDEBUG
  wc_debug(spec, INTER);
#endif

// SORT/GROUP
#ifdef DEBUG
  printf("\n##############\n");
  printf("# SORT START #\n");
  printf("##############\n");
#endif
#ifdef TIME
  start = clock();
#endif
  sort(spec, compare);
#ifdef TIME
  end = clock();
  printf("SORT/GROUP: %.2fms\n", (double)(end-start)/CLOCKS_PER_SEC*1000);
#endif
#ifdef WCDEBUG
  wc_debug(spec, INTER);
#endif

  // REDUCE
#ifdef DEBUG
  printf("\n################\n");
  printf("# REDUCER START #\n");
  printf("################\n");
#endif
#ifdef TIME
  start = clock();
#endif
  for (i = 0; i < spec->interRecords; i += spec->reduces)
    reducer(spec, reduce, i);
#ifdef TIME
  end = clock();
  printf("REDUCE: %.2fms\n", (double)(end-start)/CLOCKS_PER_SEC*1000);
#endif
#ifdef WCDEBUG
  wc_debug(spec, OUTPUT);
#endif

  // WRITE
#ifdef DEBUG
  printf("\n###############\n");
  printf("# WRITE START #\n");
  printf("###############\n");
#endif
#ifdef TIME
  start = clock();
#endif
  writer(spec, outfile);
#ifdef TIME
  end = clock();
  printf("WRITE: %.2fms\n", (double)(end-start)/CLOCKS_PER_SEC*1000);
#endif

  // clean up
#ifdef DEBUG
  printf("\n############\n");
  printf("# CLEAN UP #\n");
  printf("############\n");
#endif
  destroy_spec(spec, INPUT);
  destroy_spec(spec, INTER);
  destroy_spec(spec, SPEC);

#ifdef DEBUG
  printf("\nDONE\n");
#endif

  pthread_exit(NULL);
  return 0;
}
Beispiel #24
0
int main() {


    // Create the mapper pipes

    // Add "for" loop for each of these two

    if(pipe(mapper_pipes)) {

       perror("pipe");

       exit(EXIT_FAILURE);

    }


    // Create the reducer pipes

    if(pipe(reducer_pipes)) {

       perror("pipe");

       exit(EXIT_FAILURE);

    }


   int i;




    pid_t mapperpids[NUMBER_OF_MAPPERS];

    pid_t reducerpids[NUMBER_OF_REDUCERS];


   //Opens and checks the input file for error 

    FILE *input_file = fopen("input.txt", "r");

    if(input_file == NULL)

    {

        perror("fopen");

    }


    i = 0;


    //Writes each line of the input file to each mapper.

    char buffer[BUFFER_SIZE];

    while(fgets(buffer, BUFFER_SIZE, input_file) != NULL)

    {

        printf("line: %s\n", buffer);

        write(mapper_pipes[i][WRITE_END], buffer, strlen(buffer));

        close(mapper_pipes[i][WRITE_END]);

        i++;

    }


    //Closes the input file and checks for error.

   fclose(input_file);

    if(input_file != 0)

    {

        perror("fclose");

    }



    // Fork mappers

    for(i = 0; i < NUMBER_OF_MAPPERS; i++)

    {

        pid_t mapperpid = fork();

        if(mapperpid < 0)

        {

            perror("fork");

            exit(EXIT_FAILURE);

        }


        //if child

        else if(mapperpid == 0)

        {

            close(mapper_pipes[i][WRITE_END]);

            mapper(i, mapper_pipes[i][READ_END]);

            exit(EXIT_SUCCESS);

        }


        //else if parent

        else if(mapperpid > 0)

        {

            close(mapper_pipes[i][READ_END]);


            //current mapper pid is added to a list of mapper pids

            mapperpids[i] = mapperpid;

        }

    }

       //fork reducers

    for(i = 0; i < NUMBER_OF_REDUCERS; i++)

    {

        pid_t reducerpid = fork();

        if(reducerpid < 0) {

            perror("fork");

            exit(EXIT_FAILURE);

        }


        //if child

        else if(reducerpid == 0) {

            reducer(i, reducer_pipes[i][READ_END]);

            exit(EXIT_SUCCESS);

        }


        //if parent

        else if(reducerpid > 0) {

            close(reducer_pipes[i][WRITE_END]);


            //current reducer pid is added to a list of reducer pids

            reducerpids[i] = reducerpid;

        }

    

    }


    for(i = 0; i < sizeof(mapperpids); i++)

    {

        wait(&mapperpids[i]);

    }


    for(i = 0; i < sizeof(reducerpids); i++)

    {

        wait(&reducerpids[i]);

    }


    exit(EXIT_SUCCESS);
    }
int main(int argc, char **argv){
	FILE *file = fopen("file1","r");
	FILE *out = NULL;
	char str_buf[1024][50];
	unsigned str_buf_in = 0;
	unsigned str_buf_out = 0;
	char str[50];
	int read_finish = 0;
	int num_read = 0, num_write = 0;
	char **input_filenames = NULL;
	int input_len; //num of input files
	FILE **input_files = NULL;
	int i,j; double elapsed_time;
	int mapping_done = 0;//done when all mapper thread done
	struct timeval tvalBefore, tvalAfter;
	////locks///
	int rank, size, len;
    char name[MPI_MAX_PROCESSOR_NAME];
    omp_set_num_threads(4);
    MPI_Init(&argc, &argv);                 
    MPI_Comm_size(MPI_COMM_WORLD, &size);   
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);  
    MPI_Get_processor_name(name, &len);
    MPI_Status status;
	omp_init_lock(&worklock);
	omp_init_lock(&inclock);
	omp_init_lock(&readlock);
	omp_init_lock(&readerlock);
	omp_init_lock(&mapperlock);
	if(argc < 5){
		printf("Usage ./mapreduce -in [input files].... -out [output file]\n");
		return 0;
	}else{
		if(strcmp("-in",argv[1])){
			printf("Usage ./mapreduce -in [input files].... -out [output file]\n");
			return 0;
		}
		for(i=2;i<argc;i++){ //start from first input file
			if(!strcmp("-out",argv[i])){
				break;
			}
		}
		input_len = i - 2;
		input_filenames = (char**)malloc(sizeof(char*)*input_len);
		for(j=0;j<input_len;j++)
			input_filenames[j] = (char*)malloc(sizeof(char)*50);
		for(i=2,j=0;j<input_len;i++,j++){
			strcpy(input_filenames[j],argv[i]);
		}
		input_files = read_in(input_filenames,input_len,0);
		if(strcmp("-out",argv[2+input_len])){
			printf("output file missing, using default name 'out'\n");
			out = fopen("out","w");
		}else{
			out = fopen(argv[3+input_len],"w");
		}
	}
	omp_set_num_threads(8);
	
	fifoQ *queue_to_map = initQ(1000000, "queue_to_map");
	fifoQ *queue_to_reduce = initQ(1000000, "queue_to_map");
	fifoQ **queues_to_map = (fifoQ**)malloc(sizeof(fifoQ*)*5);
	queues_to_map[0] = initQ(1000000, "queue_to_map0");
	queues_to_map[1] = initQ(1000000, "queue_to_map1");
	queues_to_map[2] = initQ(1000000, "queue_to_map2");
	queues_to_map[3] = initQ(1000000, "queue_to_map3");
	queues_to_map[4] = initQ(1000000, "queue_to_map4");
	fifoQ **queues_to_reduce = (fifoQ**)malloc(sizeof(fifoQ*)*5);
	queues_to_reduce[0] = initQ(1000000, "queue_to_reduce0");
	queues_to_reduce[1] = initQ(1000000, "queue_to_reduce1");
	queues_to_reduce[2] = initQ(1000000, "queue_to_reduce2");
	queues_to_reduce[3] = initQ(1000000, "queue_to_reduce3");
	queues_to_reduce[4] = initQ(1000000, "queue_to_reduce4");
	fifoQ **queues_reduced = (fifoQ**)malloc(sizeof(fifoQ*)*5);
	fifoQ *final_queue = initQ(1000000, "final Q");
	
	int sendsize = input_len/size + (input_len % size - rank > 0 ? 1 : 0); //num of files send to a node
	if(rank==0){ //distribute files
		int i,j;
		char ***files_tosend = (char***)malloc(sizeof(char**)*input_len);
		int lsendsize;
		FILE **node_files;
		
		for(i=0;i<size;i++){
			lsendsize = input_len/size + (input_len % size - i > 0 ? 1 : 0); //num of files send to a node
			printf("send size of core %d is %d\n",i,lsendsize);
			files_tosend[i] = (char**)malloc(sizeof(char*)*lsendsize);
			for(j=0;j<lsendsize;j++){
				files_tosend[i][j] = (char*)malloc(sizeof(char)*50);
			}
		}
		for(i=0;i<input_len;i++){
			int belongs_to = i % size;
			int pos = i/size;
			strcpy(files_tosend[belongs_to][pos],input_filenames[i]);
			printf("distributing file %s to files_tosend %d,%d, value %s\n",input_filenames[i],belongs_to,pos,files_tosend[belongs_to][pos]);
		}
		if(size>1){
			for(i=1;i<size;i++){
				lsendsize = input_len/size + (input_len % size - i > 0 ? 1 : 0);
				for(j=0;j<lsendsize;j++){
					printf("sending %s to cpu %d\n",files_tosend[i][j],i);
					MPI_Send(files_tosend[i][j],50,MPI_BYTE,i,1,MPI_COMM_WORLD);
					printf("send done\n");
				}
			}
		}
		node_files = (FILE**)malloc(sizeof(FILE*)*sendsize);
		for(i=0;i<sendsize;i++){
			node_files[i] = fopen(files_tosend[rank][i],"r");
		}
		gettimeofday (&tvalBefore, NULL);
		#pragma omp parallel sections
		{
		
		#pragma omp section //reader thread0
		{
			int i; int odd_even = 0;
			//printf("reader 0 is core #%d\n",rank);
			for(i=0;i<sendsize;i++){
				while(!feof(node_files[i])){
	                  /////////check if full///////////
					omp_set_lock(&readerlock);
					if(!feof(node_files[i])){
						strcpy(str,"");
						fscanf(node_files[i],"%s",str);
					}
					else{
						omp_unset_lock(&readerlock);
						break;
					}
					omp_unset_lock(&readerlock);
					if(strcmp(str,""))
						putWork(queues_to_map[0], constr_work(str));
				}
			}
			omp_set_lock(&inclock);
			read_finish++;
			omp_unset_lock(&inclock);
			//printf("reader thread0 done\n");
		}
		#pragma omp section //reader thread1
		{
			int i; int odd_even = 0;
			//printf("reader 1 is core #%d\n",rank);
			for(i=0;i<sendsize;i++){
				while(!feof(node_files[i])){
	                  /////////check if full///////////
					omp_set_lock(&readerlock);
					if(!feof(node_files[i])){
						strcpy(str,"");
						fscanf(node_files[i],"%s",str);
					}
					else{
						omp_unset_lock(&readerlock);
						break;
					}
					omp_unset_lock(&readerlock);
					if(strcmp(str,""))
						putWork(queues_to_map[1], constr_work(str));
				}
			}
			omp_set_lock(&inclock);
			read_finish++;
			omp_unset_lock(&inclock);
			//printf("reader thread1 done\n");
		}
		#pragma omp section //reader thread2
		{
			int i; int odd_even = 0;
			//printf("reader 2 is core #%d\n",rank);
			for(i=0;i<sendsize;i++){
				while(!feof(node_files[i])){
	                  /////////check if full///////////
					omp_set_lock(&readerlock);
					if(!feof(node_files[i])){
						strcpy(str,"");
						fscanf(node_files[i],"%s",str);
					}
					else{
						omp_unset_lock(&readerlock);
						break;
					}
					omp_unset_lock(&readerlock);
					if(strcmp(str,""))
						putWork(queues_to_map[2], constr_work(str));
				}
			}
			omp_set_lock(&inclock);
			read_finish++;
			omp_unset_lock(&inclock);
			//printf("reader thread2 done\n");
		}
		#pragma omp section //reader thread3
		{
			//	printf("reader 3 is core #%d\n",rank);
			int i; int odd_even = 0;
			for(i=0;i<sendsize;i++){
				while(!feof(node_files[i])){
	                  /////////check if full///////////
					omp_set_lock(&readerlock);
					if(!feof(node_files[i])){
						strcpy(str,"");
						fscanf(node_files[i],"%s",str);
					}
					else{
						omp_unset_lock(&readerlock);
						break;
					}
					omp_unset_lock(&readerlock);
					if(strcmp(str,""))
						putWork(queues_to_map[3], constr_work(str));
				}
			}
			omp_set_lock(&inclock);
			read_finish++;
			omp_unset_lock(&inclock);
			//printf("reader thread3 done %d\n",rank);
		}
		#pragma omp section //mapper thread 0
		{
			int i;
			fifoQ *innerQ = initQ(50000,"innerQ 0");
			while(read_finish<NUM_READ_THREADS || !is_empty(queues_to_map[0])){
				printf("");
				if(!is_empty(queues_to_map[0])){
					work work = getWork(queues_to_map[0]);
					//mapper(queues_to_reduce[hash(work.str)], work);
					mapper(innerQ, work);
				}
			}
			for(i=0;i<=innerQ->in;i++){
				work work = getWork(innerQ);
				putWork(queues_to_reduce[hash(work.str)],work);
			}
			omp_set_lock(&inclock);
			mapping_done++;
			omp_unset_lock(&inclock);
			//printf("mapper thread0 done %d\n",rank);
			gettimeofday (&tvalAfter, NULL);
    elapsed_time = (float)(tvalAfter.tv_sec - tvalBefore.tv_sec)+((float)(tvalAfter.tv_usec - tvalBefore.tv_usec)/1000000);
    if(rank==0)
		printf("elapsed time = %.2f sec,rank %d in map 0\n",elapsed_time,rank);
		}
		#pragma omp section //mapper thread 1
		{
			int i;
			fifoQ *innerQ = initQ(50000,"innerQ 1");
			while(read_finish<NUM_READ_THREADS || !is_empty(queues_to_map[1])){
				printf("");
				if(!is_empty(queues_to_map[1])){		
					work work = getWork(queues_to_map[1]);				
					//mapper(queues_to_reduce[hash(work.str)], work);
					mapper(innerQ, work);
				}
			}
			for(i=0;i<=innerQ->in;i++){
				work work = getWork(innerQ);
				putWork(queues_to_reduce[hash(work.str)],work);
			}
			omp_set_lock(&inclock);
			mapping_done++;
			omp_unset_lock(&inclock);
			//printf("mapper thread1 done %d\n",rank);
			gettimeofday (&tvalAfter, NULL);
    elapsed_time = (float)(tvalAfter.tv_sec - tvalBefore.tv_sec)+((float)(tvalAfter.tv_usec - tvalBefore.tv_usec)/1000000);
    if(rank==0)
		printf("elapsed time = %.2f sec,rank %d in map 1\n",elapsed_time,rank);
		}
		#pragma omp section //mapper thread 2
		{
			int i;
			fifoQ *innerQ = initQ(50000,"innerQ 2");
			while(read_finish<NUM_READ_THREADS || !is_empty(queues_to_map[2])){
				printf("");
				if(!is_empty(queues_to_map[2])){		
					work work = getWork(queues_to_map[2]);				
					//mapper(queues_to_reduce[hash(work.str)], work);
					mapper(innerQ, work);
				}
			}
			for(i=0;i<=innerQ->in;i++){
				work work = getWork(innerQ);
				putWork(queues_to_reduce[hash(work.str)],work);
			}
			omp_set_lock(&inclock);
			mapping_done++;
			omp_unset_lock(&inclock);
			//printf("mapper thread2 done %d\n",rank);
			gettimeofday (&tvalAfter, NULL);
    elapsed_time = (float)(tvalAfter.tv_sec - tvalBefore.tv_sec)+((float)(tvalAfter.tv_usec - tvalBefore.tv_usec)/1000000);
    if(rank==0)
		printf("elapsed time = %.2f sec,rank %d in map 2\n",elapsed_time,rank);
		}
		#pragma omp section //mapper thread 3
		{
			int i;
			fifoQ *innerQ = initQ(50000,"innerQ 2");
			while(read_finish<NUM_READ_THREADS || !is_empty(queues_to_map[3])){
				printf("");
				if(!is_empty(queues_to_map[3])){		
					work work = getWork(queues_to_map[3]);				
					//mapper(queues_to_reduce[hash(work.str)], work);
					mapper(innerQ, work);
				}
			}
			for(i=0;i<=innerQ->in;i++){
				work work = getWork(innerQ);
				putWork(queues_to_reduce[hash(work.str)],work);
			}
			omp_set_lock(&inclock);
			mapping_done++;
			omp_unset_lock(&inclock);
			//printf("mapper thread3 done %d\n",rank);
			gettimeofday (&tvalAfter, NULL);
    elapsed_time = (float)(tvalAfter.tv_sec - tvalBefore.tv_sec)+((float)(tvalAfter.tv_usec - tvalBefore.tv_usec)/1000000);
    if(rank==0)
		printf("elapsed time = %.2f sec,rank %d in map 3\n",elapsed_time,rank);
		}
		#pragma omp section //reducer thread 0 
		{
			int i;
			gettimeofday (&tvalAfter, NULL);
    elapsed_time = (float)(tvalAfter.tv_sec - tvalBefore.tv_sec)+((float)(tvalAfter.tv_usec - tvalBefore.tv_usec)/1000000);
    if(rank==0)
		printf("elapsed time = %.2f sec,rank %d\n",elapsed_time,rank);
			while(mapping_done<NUM_READ_THREADS){
				printf("");
			}
			gettimeofday (&tvalAfter, NULL);
    elapsed_time = (float)(tvalAfter.tv_sec - tvalBefore.tv_sec)+((float)(tvalAfter.tv_usec - tvalBefore.tv_usec)/1000000);
    if(rank==0)
		printf("elapsed time = %.2f sec,rank %d\n",elapsed_time,rank);
			queues_reduced[0] = reducer(queues_to_reduce[0]);
			//printf("reducer thread 0 done\n");
			gettimeofday (&tvalAfter, NULL);
    elapsed_time = (float)(tvalAfter.tv_sec - tvalBefore.tv_sec)+((float)(tvalAfter.tv_usec - tvalBefore.tv_usec)/1000000);
    if(rank==0)
		printf("elapsed time = %.2f sec,rank %d\n",elapsed_time,rank);
		}
		#pragma omp section //reducer thread 1
		{
			int i;
			while(mapping_done<NUM_READ_THREADS){printf("");}
			queues_reduced[1] = reducer(queues_to_reduce[1]);
			//printf("reducer thread 1 done\n");
		}
		#pragma omp section //reducer thread 2 
		{
			int i;
			while(mapping_done<NUM_READ_THREADS){printf("");}
			queues_reduced[2] = reducer(queues_to_reduce[2]);
			//printf("reducer thread 2 done\n");
		}
		#pragma omp section //reducer thread 3
		{
			int i;
			while(mapping_done<NUM_READ_THREADS){printf("");}
			queues_reduced[3] = reducer(queues_to_reduce[3]);
			//printf("reducer thread 3 done\n");
		}
		}
		gettimeofday (&tvalAfter, NULL);
    elapsed_time = (float)(tvalAfter.tv_sec - tvalBefore.tv_sec)+((float)(tvalAfter.tv_usec - tvalBefore.tv_usec)/1000000);
    if(rank==0)
		printf("elapsed time = %.2f sec,rank %d\n",elapsed_time,rank);
	}
	else{
		int i;
		FILE** node_files = (FILE**)malloc(sizeof(FILE*)*sendsize);
		for(i=0;i<sendsize;i++){
			char *bufstr = (char*)malloc(sizeof(char)*50);
			MPI_Recv(bufstr,50,MPI_BYTE, 0,1, MPI_COMM_WORLD, &status);
			//printf("%s received\n",bufstr);
			node_files[i] = fopen(bufstr,"r");
		}
		#pragma omp parallel sections shared(input_files) private(str)
		{	
		//printf("using %d threads in core %d\n",omp_get_num_threads(),rank);
		#pragma omp section //reader thread0
		{
			int i; int odd_even = 0;
		//	printf("reader 0 is core #%d\n",rank);
			for(i=0;i<sendsize;i++){
				while(!feof(node_files[i])){
	                  /////////check if full///////////
					omp_set_lock(&readerlock);
					if(!feof(node_files[i])){
						strcpy(str,"");
						fscanf(node_files[i],"%s",str);
					}
					else{
						omp_unset_lock(&readerlock);
						break;
					}
					omp_unset_lock(&readerlock);
					if(strcmp(str,""))
						putWork(queues_to_map[0], constr_work(str));
				}
			}
			omp_set_lock(&inclock);
			read_finish++;
			omp_unset_lock(&inclock);
			//printf("reader thread0 done\n");
		}
		#pragma omp section //reader thread1
		{
			int i; int odd_even = 0;
		//	printf("reader 1 is core #%d\n",rank);
			for(i=0;i<sendsize;i++){
				while(!feof(node_files[i])){
	                  /////////check if full///////////
					omp_set_lock(&readerlock);
					if(!feof(node_files[i])){
						strcpy(str,"");
						fscanf(node_files[i],"%s",str);
					}
					else{
						omp_unset_lock(&readerlock);
						break;
					}
					omp_unset_lock(&readerlock);
					if(strcmp(str,""))
						putWork(queues_to_map[1], constr_work(str));
				}
			}
			omp_set_lock(&inclock);
			read_finish++;
			omp_unset_lock(&inclock);
			//printf("reader thread1 done\n");
		}
		#pragma omp section //reader thread2
		{
			int i; int odd_even = 0;
			//printf("reader 2 is core #%d\n",rank);
			for(i=0;i<sendsize;i++){
				while(!feof(node_files[i])){
	                  /////////check if full///////////
					omp_set_lock(&readerlock);
					if(!feof(node_files[i])){
						strcpy(str,"");
						fscanf(node_files[i],"%s",str);
					}
					else{
						omp_unset_lock(&readerlock);
						break;
					}
					omp_unset_lock(&readerlock);
					if(strcmp(str,""))
						putWork(queues_to_map[2], constr_work(str));
				}
			}
			omp_set_lock(&inclock);
			read_finish++;
			omp_unset_lock(&inclock);
			//printf("reader thread2 done\n");
		}
		#pragma omp section //reader thread3
		{
				//printf("reader 3 is core #%d\n",rank);
			int i; int odd_even = 0;
			for(i=0;i<sendsize;i++){
				while(!feof(node_files[i])){
	                  /////////check if full///////////
					omp_set_lock(&readerlock);
					if(!feof(node_files[i])){
						strcpy(str,"");
						fscanf(node_files[i],"%s",str);
					}
					else{
						omp_unset_lock(&readerlock);
						break;
					}
					omp_unset_lock(&readerlock);
					if(strcmp(str,""))
						putWork(queues_to_map[3], constr_work(str));
				}
			}
			omp_set_lock(&inclock);
			read_finish++;
			omp_unset_lock(&inclock);
			//printf("reader thread3 done %d\n",rank);
		}
		#pragma omp section //mapper thread 0
		{
		int i;
		fifoQ *innerQ = initQ(50000,"innerQ 0");
		//printf("map1\n");
		while(read_finish<NUM_READ_THREADS || !is_empty(queues_to_map[0])){
			printf("");
			if(!is_empty(queues_to_map[0])){
				work work = getWork(queues_to_map[0]);
				//mapper(queues_to_reduce[hash(work.str)], work);
				mapper(innerQ, work);
			}
		}
		for(i=0;i<=innerQ->in;i++){
			work work = getWork(innerQ);
			putWork(queues_to_reduce[hash(work.str)],work);
		}
		omp_set_lock(&inclock);
		mapping_done++;
		omp_unset_lock(&inclock);
		//printf("mapper thread0 done %d\n",rank);
		}
		#pragma omp section //mapper thread 1
		{
			int i;
			fifoQ *innerQ = initQ(50000,"innerQ 1");
			while(read_finish<NUM_READ_THREADS || !is_empty(queues_to_map[1])){
				printf("");
				if(!is_empty(queues_to_map[1])){		
					work work = getWork(queues_to_map[1]);				
					//mapper(queues_to_reduce[hash(work.str)], work);
					mapper(innerQ, work);
				}
			}
			for(i=0;i<=innerQ->in;i++){
				work work = getWork(innerQ);
				putWork(queues_to_reduce[hash(work.str)],work);
			}
			omp_set_lock(&inclock);
			mapping_done++;
			omp_unset_lock(&inclock);
			//printf("mapper thread1 done %d\n",rank);
		}
		#pragma omp section //mapper thread 2
		{
			int i;
			fifoQ *innerQ = initQ(50000,"innerQ 2");
			while(read_finish<NUM_READ_THREADS || !is_empty(queues_to_map[2])){
				printf("");
				if(!is_empty(queues_to_map[2])){		
					work work = getWork(queues_to_map[2]);				
					//mapper(queues_to_reduce[hash(work.str)], work);
					mapper(innerQ, work);
				}
			}
			for(i=0;i<=innerQ->in;i++){
				work work = getWork(innerQ);
				putWork(queues_to_reduce[hash(work.str)],work);
			}
			omp_set_lock(&inclock);
			mapping_done++;
			omp_unset_lock(&inclock);
			//printf("mapper thread2 done %d\n",rank);
		}
		#pragma omp section //mapper thread 3
		{
			int i;
			fifoQ *innerQ = initQ(50000,"innerQ 2");
			while(read_finish<NUM_READ_THREADS || !is_empty(queues_to_map[3])){
				printf("");
				if(!is_empty(queues_to_map[3])){			
					work work = getWork(queues_to_map[3]);				
					//mapper(queues_to_reduce[hash(work.str)], work);
					mapper(innerQ, work);
				}
			}
			for(i=0;i<=innerQ->in;i++){
				work work = getWork(innerQ);
				putWork(queues_to_reduce[hash(work.str)],work);
			}
			omp_set_lock(&inclock);
			mapping_done++;
			omp_unset_lock(&inclock);
			//printf("mapper thread3 done %d\n",rank);
		}
		#pragma omp section //reducer thread 0 
		{
			int i;
			while(mapping_done<NUM_READ_THREADS){
				printf("");
			}
			queues_reduced[0] = reducer(queues_to_reduce[0]);
			//printf("reducer thread 0 done\n");
		}
		#pragma omp section //reducer thread 1
		{
			int i;
			while(mapping_done<NUM_READ_THREADS){printf("");}
			queues_reduced[1] = reducer(queues_to_reduce[1]);
			//printf("reducer thread 1 done\n");
		}
		#pragma omp section //reducer thread 2 
		{
			int i;
			while(mapping_done<NUM_READ_THREADS){printf("");}
			queues_reduced[2] = reducer(queues_to_reduce[2]);
			//printf("reducer thread 2 done\n");
		}
		#pragma omp section //reducer thread 3
		{
			int i;
			while(mapping_done<NUM_READ_THREADS){printf("");}
			queues_reduced[3] = reducer(queues_to_reduce[3]);
			//printf("reducer thread 3 done\n");
		}
		
		}
	}
	MPI_Barrier(MPI_COMM_WORLD);
	gettimeofday (&tvalAfter, NULL);
    elapsed_time = (float)(tvalAfter.tv_sec - tvalBefore.tv_sec)+((float)(tvalAfter.tv_usec - tvalBefore.tv_usec)/1000000);
    if(rank==0)
		printf("elapsed time = %.2f sec,rank %d\n",elapsed_time,rank);
	if(rank==0){ //final reducuction
		int i,j,revbuf;int mainct;
		for(i=0;i<NUM_READ_THREADS;i++){
			combine_queue(final_queue,queues_reduced[i]);
		}
		//printf("main node has %d to final reduce\n",calcnum(queues_reduced,NUM_READ_THREADS));
		for(i=1;i<size;i++){
			MPI_Recv(&revbuf,1,MPI_INT,i,1,MPI_COMM_WORLD,&status);
			//printf("need to receive %d strings from node %d\n",revbuf,i);
			char *strbuf = (char*)malloc(sizeof(char)*50);
			char ctbuf = 0;
			for(j=0;j<revbuf;j++){
				MPI_Recv(strbuf,50,MPI_BYTE,i,1,MPI_COMM_WORLD,&status);
				MPI_Recv(&ctbuf,50,MPI_INT,i,1,MPI_COMM_WORLD,&status);
				work work;
				strcpy(work.str,strbuf);
				work.count = ctbuf;
				//printf("received <%s,%d> from node %d\n",work.str,work.count,i);
				putWork(final_queue,work);
			}
		}
		fifoQ *output = reducer(final_queue);
		printQ_to_file(&output,1,out);
	}else{
		int i,total_num;
		total_num = calcnum(queues_reduced,NUM_READ_THREADS);
		MPI_Send(&total_num,1,MPI_INT,0,1,MPI_COMM_WORLD);
		for(i=0;i<NUM_READ_THREADS;i++){
			combine_queue(final_queue,queues_reduced[i]);
		}
		for(i=0;i<total_num;i++){
			MPI_Send(&final_queue->works[i].str,50,MPI_BYTE,0,1,MPI_COMM_WORLD);
			MPI_Send(&final_queue->works[i].count,1,MPI_INT,0,1,MPI_COMM_WORLD);
		}
	}
	
	for(i=0;i<input_len;i++){
		fclose(input_files[i]);
	}
	fclose(out);
	/*printQ(queues_to_map[0]);
	printQ(queues_to_map[1]);
	printQ(queues_to_map[2]);
	printQ(queues_to_map[3]);*/
	/*printQ(queues_reduced[0]);
	printQ(queues_reduced[1]);
	printQ(queues_reduced[2]);
	printQ(queues_reduced[3]);*/
	omp_destroy_lock(&inclock);
	omp_destroy_lock(&worklock);
	omp_destroy_lock(&readlock);
	omp_destroy_lock(&readerlock);
	omp_destroy_lock(&mapperlock);
	MPI_Finalize();

	return 0;
}
Beispiel #26
0
int main(int argc, char** argv)
{
  utils::mpi_world mpi_world(argc, argv);
  
  const int mpi_rank = MPI::COMM_WORLD.Get_rank();
  const int mpi_size = MPI::COMM_WORLD.Get_size();
  
  try {
    if (getoptions(argc, argv) != 0) 
      return 1;

    if (command.empty())
      throw std::runtime_error("no command?");

    typedef MapReduce map_reduce_type;
      
    typedef map_reduce_type::id_type         id_type;
    typedef map_reduce_type::value_type      value_type;
    typedef map_reduce_type::queue_type      queue_type;
    typedef map_reduce_type::queue_id_type   queue_id_type;
    typedef map_reduce_type::subprocess_type subprocess_type;

    typedef Mapper  mapper_type;
    typedef Reducer reducer_type;
    
    typedef Consumer consumer_type;
    typedef Merger   merger_type;
    
    if (mpi_rank == 0) {
      subprocess_type subprocess(command);
      
      queue_type    queue_is(mpi_size);
      queue_type    queue_send(1);
      queue_id_type queue_id;
      queue_type    queue_recv;
      
      const bool flush_output = (output_file == "-"
				 || (boost::filesystem::exists(output_file)
				     && ! boost::filesystem::is_regular_file(output_file)));
      
      utils::compress_istream is(input_file, 1024 * 1024);
      utils::compress_ostream os(output_file, 1024 * 1024 * (! flush_output));

      boost::thread consumer(consumer_type(queue_is, is));
      boost::thread merger(merger_type(queue_recv, os, mpi_size));

      boost::thread mapper(mapper_type(queue_send, queue_id, subprocess));
      boost::thread reducer(reducer_type(queue_recv, queue_id, subprocess));
      
      typedef utils::mpi_ostream        ostream_type;
      typedef utils::mpi_istream_simple istream_type;

      typedef boost::shared_ptr<ostream_type> ostream_ptr_type;
      typedef boost::shared_ptr<istream_type> istream_ptr_type;
      
      typedef std::vector<ostream_ptr_type, std::allocator<ostream_ptr_type> > ostream_ptr_set_type;
      typedef std::vector<istream_ptr_type, std::allocator<istream_ptr_type> > istream_ptr_set_type;
      
      ostream_ptr_set_type ostream(mpi_size);
      istream_ptr_set_type istream(mpi_size);
      
      for (int rank = 1; rank < mpi_size; ++ rank) {
	ostream[rank].reset(new ostream_type(rank, line_tag, 4096));
	istream[rank].reset(new istream_type(rank, line_tag, 4096));
      }
      
      std::string line;
      value_type value(0, std::string());
      value_type value_recv(0, std::string());
      
      int non_found_iter = 0;
      
      while (value.first != id_type(-1)) {
	bool found = false;
	
	for (int rank = 1; rank < mpi_size && value.first != id_type(-1); ++ rank)
	  if (ostream[rank]->test() && queue_is.pop(value, true) && value.first != id_type(-1)) {
	    ostream[rank]->write(utils::lexical_cast<std::string>(value.first) + ' ' + value.second);
	    
	    found = true;
	  }
	
	if (queue_send.empty() && queue_is.pop(value, true) && value.first != id_type(-1)) {
	  queue_send.push(value);
	  
	  found = true;
	}
	
	// reduce...
	for (int rank = 1; rank < mpi_size; ++ rank)
	  if (istream[rank] && istream[rank]->test()) {
	    if (istream[rank]->read(line)) {
	      tokenize(line, value_recv);
	      
	      queue_recv.push_swap(value_recv);
	    } else {
	      queue_recv.push(std::make_pair(id_type(-1), std::string()));
	      istream[rank].reset();
	    }
	    
	    found = true;
	  }
	
	non_found_iter = loop_sleep(found, non_found_iter);
      }
      
      bool terminated = false;
      
      for (;;) {
	bool found = false;
	
	if (! terminated && queue_send.push(std::make_pair(id_type(-1), std::string()), true)) {
	  terminated = true;
	  
	  found = true;
	}
	
	// termination...
	for (int rank = 1; rank < mpi_size; ++ rank)
	  if (ostream[rank] && ostream[rank]->test()) {
	    if (! ostream[rank]->terminated())
	      ostream[rank]->terminate();
	    else
	      ostream[rank].reset();
	    
	    found = true;
	  }
	
	// reduce...
	for (int rank = 1; rank < mpi_size; ++ rank)
	  if (istream[rank] && istream[rank]->test()) {
	    if (istream[rank]->read(line)) {
	      tokenize(line, value_recv);
	      
	      queue_recv.push_swap(value_recv);
	    } else {
	      queue_recv.push(std::make_pair(id_type(-1), std::string()));
	      istream[rank].reset();
	    }
	    
	    found = true;
	  }
	
	// termination condition!
	if (std::count(istream.begin(), istream.end(), istream_ptr_type()) == mpi_size
	    && std::count(ostream.begin(), ostream.end(), ostream_ptr_type()) == mpi_size
	    && terminated) break;
	
	non_found_iter = loop_sleep(found, non_found_iter);
      }
      
      mapper.join();
      reducer.join();
      consumer.join();
      merger.join();
    } else {
      subprocess_type subprocess(command);
      
      queue_type    queue_send(1);
      queue_id_type queue_id;
      queue_type    queue_recv;
      
      boost::thread mapper(mapper_type(queue_send, queue_id, subprocess));
      boost::thread reducer(reducer_type(queue_recv, queue_id, subprocess));

      typedef utils::mpi_istream        istream_type;
      typedef utils::mpi_ostream_simple ostream_type;
      
      boost::shared_ptr<istream_type> is(new istream_type(0, line_tag, 4096));
      boost::shared_ptr<ostream_type> os(new ostream_type(0, line_tag, 4096));
      
      std::string line;
      value_type value;

      bool terminated = false;
      
      int non_found_iter = 0;
      for (;;) {
	bool found = false;
	
	if (is && is->test() && queue_send.empty()) {
	  if (is->read(line))
	    tokenize(line, value);
	  else {
	    value.first  = id_type(-1);
	    value.second = std::string();
	    
	    is.reset();
	  }
	  
	  queue_send.push_swap(value);
	  
	  found = true;
	}
	
	if (! terminated) {
	  if (os && os->test() && queue_recv.pop_swap(value, true)) {
	    if (value.first == id_type(-1))
	      terminated = true;
	    else
	      os->write(utils::lexical_cast<std::string>(value.first) + ' ' + value.second);
	    
	    found = true;
	  }
	} else {
	  if (os && os->test()) {
	    if (! os->terminated())
	      os->terminate();
	    else
	      os.reset();
	    found = true;
	  }
	}
	
	if (! is && ! os) break;
	
	non_found_iter = loop_sleep(found, non_found_iter);
      }
      
      mapper.join();
      reducer.join();
    }
    
    // synchronize...
    if (mpi_rank == 0) {
      std::vector<MPI::Request, std::allocator<MPI::Request> > request_recv(mpi_size);
      std::vector<MPI::Request, std::allocator<MPI::Request> > request_send(mpi_size);
      std::vector<bool, std::allocator<bool> > terminated_recv(mpi_size, false);
      std::vector<bool, std::allocator<bool> > terminated_send(mpi_size, false);
    
      terminated_recv[0] = true;
      terminated_send[0] = true;
      for (int rank = 1; rank != mpi_size; ++ rank) {
	request_recv[rank] = MPI::COMM_WORLD.Irecv(0, 0, MPI::INT, rank, notify_tag);
	request_send[rank] = MPI::COMM_WORLD.Isend(0, 0, MPI::INT, rank, notify_tag);
      }
    
      int non_found_iter = 0;
      for (;;) {
	bool found = false;
      
	for (int rank = 1; rank != mpi_size; ++ rank)
	  if (! terminated_recv[rank] && request_recv[rank].Test()) {
	    terminated_recv[rank] = true;
	    found = true;
	  }
      
	for (int rank = 1; rank != mpi_size; ++ rank)
	  if (! terminated_send[rank] && request_send[rank].Test()) {
	    terminated_send[rank] = true;
	    found = true;
	  }
      
	if (std::count(terminated_send.begin(), terminated_send.end(), true) == mpi_size
	    && std::count(terminated_recv.begin(), terminated_recv.end(), true) == mpi_size) break;
      
	non_found_iter = loop_sleep(found, non_found_iter);
      }
    } else {
      MPI::Request request_send = MPI::COMM_WORLD.Isend(0, 0, MPI::INT, 0, notify_tag);
      MPI::Request request_recv = MPI::COMM_WORLD.Irecv(0, 0, MPI::INT, 0, notify_tag);
    
      bool terminated_send = false;
      bool terminated_recv = false;
    
      int non_found_iter = 0;
      for (;;) {
	bool found = false;
      
	if (! terminated_send && request_send.Test()) {
	  terminated_send = true;
	  found = true;
	}
      
	if (! terminated_recv && request_recv.Test()) {
	  terminated_recv = true;
	  found = true;
	}
      
	if (terminated_send && terminated_recv) break;
      
	non_found_iter = loop_sleep(found, non_found_iter);
      }
    }
  }
  catch (const std::exception& err) {
    std::cerr << "error: " << argv[0] << " "<< err.what() << std::endl;
    MPI::COMM_WORLD.Abort(1);
    return 1;
  }
  return 0;
}
Beispiel #27
0
void draw_spike_3d()
{
	extern gfloat det_axis_angle;
	gdk_threads_enter();
	gdk_window_copy_area(main_pixmap,gc,
			0,0,
			main_pixmap,
			xdet_scroll,
			zdet_scroll,
			width-xdet_scroll,
			height-zdet_scroll);
	if (xdet_scroll > 0)
	{
		gdk_draw_rectangle(main_pixmap,
				main_display->style->black_gc,
				TRUE, width-xdet_scroll,0,
				xdet_scroll,height);
	}
	else
	{
		gdk_draw_rectangle(main_pixmap,
				main_display->style->black_gc,
				TRUE, 0,0,
				abs(xdet_scroll),height);
	}

	if (zdet_scroll > 0)
	{
		gdk_draw_rectangle(main_pixmap,
				main_display->style->black_gc,
				TRUE, 0,height-zdet_scroll,
				width,zdet_scroll);
	}
	else
	{
		gdk_draw_rectangle(main_pixmap,
				main_display->style->black_gc,
				TRUE, 0,0,
				width,abs(zdet_scroll));
	}
	gdk_threads_leave();

	/* in pixels */
	x_draw_width = width - abs(xdet_scroll)-2*border;
	y_draw_height = height - abs(zdet_scroll)-2*border;

	/* in pixels */
	x_offset = (width-x_draw_width)/2;
	y_offset = (height-y_draw_height)/2;


	det_axis_angle_deg = det_axis_angle*90/M_PI_2;
	if (det_axis_angle_deg < 0.0)
		det_axis_angle_deg += 360.0;

	dir_angle_rad = (float)atan2((float)zdet_scroll,-(float)xdet_scroll);
	dir_angle_deg = dir_angle_rad*90/M_PI_2;
	if (dir_angle_deg < 0.0)
		dir_angle_deg += 360.0;


	if (spiketilt == FALSE)
	{
		x_tilt=0.0;
		y_tilt=0.0;
	}
	else
	{
		/* Perspecitve Tilting algorithm.  Probably severely flawed
		 * but it displays nicely..  
		 * 
		 * Note to self.  Learn how to use Trig properly... :)
		 */ 
		if ((dir_angle_deg >= 315.0) || (dir_angle_deg < 45.0))
		{
			//printf("Quad 4<->1\n");
			x_tilt = sin(dir_angle_rad);
			y_tilt = cos(dir_angle_rad);
		}
		else if ((dir_angle_deg >= 45.0) && (dir_angle_deg < 135.0))
		{
			//printf("Quad 1<->2\n");
			x_tilt = cos(dir_angle_rad);
			y_tilt = sin(dir_angle_rad);
		}
		else if ((dir_angle_deg >= 135.0) && (dir_angle_deg < 225.0))
		{
			//printf("Quad 2<->3\n");
			x_tilt = -sin(dir_angle_rad);
			y_tilt = -cos(dir_angle_rad);
		}
		else if  ((dir_angle_deg >= 225.0) && (dir_angle_deg < 315.0))
		{
			//printf("Quad 3<->4\n");
			x_tilt = -cos(dir_angle_rad);
			y_tilt = -sin(dir_angle_rad);
		}
	}

	x_axis_tilt = sin(det_axis_angle);
	y_axis_tilt = cos(det_axis_angle);

	x_tilt_weight = cos(det_axis_angle);
	y_tilt_weight = 0.0;
	x_axis_weight = 1.0;
	y_axis_weight = 1.0;

/*
	// Debugging Code....  

	printf("axis angle in deg is %f\n",det_axis_angle_deg);
	printf("direction angle in deg is %f\n",dir_angle_deg);
	printf("\nx_tilt %f, y_tilt %f\n",x_tilt,y_tilt);
	printf("x_tilt_weight %f, y_tilt_weight %f\n",x_tilt_weight,y_tilt_weight);
	printf("Scroll Tilt components (%f,%f)\n",x_tilt*x_tilt_weight,y_tilt*y_tilt_weight);
	printf("x_axis_tilt %f, y_axis_tilt %f\n",x_axis_tilt,y_axis_tilt);
	printf("x_axis_weight %f, y_axis_weight %f\n",x_axis_weight,y_axis_weight);
	printf("Perspective Tilt components (%f,%f)\n\n",x_axis_tilt*x_axis_weight,y_axis_tilt*y_axis_weight);
	
*/

	/* in pixels */
	start_x = width-((x_draw_width)*(1.0-xdet_end))-x_offset;
	end_x = width-((x_draw_width)*(1.0-xdet_start))-x_offset;
	start_y = height-((y_draw_height)*(1.0-ydet_end))-y_offset;
	end_y = height-((y_draw_height)*(1.0-ydet_start))-y_offset;

	/* in pixels */
	start_x = width-((x_draw_width)*(1.0-xdet_end))-x_offset;
	end_x = width-((x_draw_width)*(1.0-xdet_start))-x_offset;
	start_y = height-((y_draw_height)*(1.0-ydet_end))-y_offset;
	end_y = height-((y_draw_height)*(1.0-ydet_start))-y_offset;
	/* end_x - start_x is X screen space in pixels 
	 * the idea is to reduce the number of gdk_draw_lines to 
	 * the number in axis_length, instead of nsamp/2. which saves drawing 
	 * over the same location on the screen and wasting time.
	 * I guess when combining pips together we should AVG them. What
	 * other way might be better? (suggestions welcome...)
	 */

	/* disp_val[] is a malloc'd array storing pip values for ALL pips, 
	 * (1/2 NSAMP). pip_arr[] is malloc'd array that is length 
	 * "axis_length"and contains anti-aliased reduction of 
	 * disp[val] using linear interpolation to combine/average 
	 * multiple pip values into one viewable spike. Should be 
	 * fully scaling with best results of having bins_per_pip 
	 * being a multiple of nsamp/2.
	 */
	axis_length = (gint)sqrt(((end_x-start_x)*(end_x-start_x))+((end_y-start_y)*(end_y-start_y)));
	loc_bins_per_pip = ((float)nsamp/2.0)/(fabs(axis_length));

	reducer(low_freq, high_freq, axis_length);
	gdk_threads_enter();

	/* Do this here instead of in the loop  axis_length number of times..
	 * cpu savings... :)
	 */
	x_amplitude = (x_tilt*x_tilt_weight)+(x_axis_tilt*x_axis_weight);
	y_amplitude = (y_tilt*y_tilt_weight)+(y_axis_tilt*y_axis_weight);

	for( i=0; i < axis_length; i++ )
	{
		pt[0].x=width-(((i*x_draw_width)*(1-xdet_start))/axis_length)\
				-((((axis_length-i)*x_draw_width)*(1-xdet_end))\
				/(axis_length))-x_offset;
		pt[0].y=height-(((i*loc_bins_per_pip)*y_draw_height*ydet_start)\
				/(nsamp/2))-((((nsamp/2)-(i*loc_bins_per_pip))\
				*y_draw_height*ydet_end)/(nsamp/2))\
				-y_offset;
		pt[1].x=pt[0].x -(gint)pip_arr[i]*x_amplitude;
		pt[1].y=pt[0].y -(gint)pip_arr[i]*y_amplitude; \
		lvl=abs((gint)pip_arr[i]*4);
		if (lvl > (MAXBANDS-1))
			lvl=(MAXBANDS-1);
		else if (lvl <= 0)
			lvl = 0;

		gdk_gc_set_foreground(gc,&colortab[16][lvl]);

		gdk_draw_line(main_pixmap,gc,\
				pt[0].x,\
				pt[0].y,\
				pt[1].x,\
				pt[1].y);
	}
	gdk_window_clear(main_display->window);

	gdk_threads_leave();
}
Beispiel #28
0
void* reduce(ArrayUtil util, ReducerFunc* reducer, void* hint, void* intialValue){
	for (int i = 0; i < util.length; ++i){
		intialValue =  reducer(hint,intialValue,(util.base+i*util.typeSize));
	}
	return intialValue;
};