/* * Joins slave processes. */ void join_slaves(void) { int i; kill_slaves(); /* Join slaves. */ for (i = 0; i < nclusters; i++) { data_receive(infd[i], &slave[i], sizeof(uint64_t)); mppa_waitpid(pids[i], NULL, 0); } }
/* * Receives work from master process. */ static void getwork(void) { int i; ssize_t n; /* Bytes to send/receive. */ ssize_t count; /* Bytes actually sent/received. */ timer_init(); n = sizeof(int); count = mppa_read(infd, &lnpoints, n); assert(n == count); data_receive(infd, &nprocs, sizeof(int)); data_receive(infd, &ncentroids, sizeof(int)); data_receive(infd, &mindistance, sizeof(int)); data_receive(infd, &dimension, sizeof(int)); n = nprocs*sizeof(int); count = mppa_read(infd, lncentroids, n); assert(count != -1); n = dimension*sizeof(float); for (i = 0; i < lnpoints; i++) data_receive(infd, &points[i*dimension], n); n = ncentroids*dimension*sizeof(float); count = mppa_read(infd, centroids, n); assert(n == count); n = lnpoints*sizeof(int); count = mppa_read(infd, map, n); assert(n == count); }
// 客户端断开,如果没有收到Logout的,模拟一个Logout命令 void MmsServer::fd_close(int fd) { Item_data_ptr state = get_state(fd); if (!state) return; YYJson_Logout cmd; cmd.yyID = state->yyID; std::string str; if (YYmmsJson::FormatCmd(&cmd, str)) { fastbuffer * writebuffer = new fastbuffer; if (writebuffer) { writebuffer->append(str.c_str(), str.length()); data_receive(fd, writebuffer, g_CFG.port); } } }
static void getwork(void) { data_receive(infd, &tasksize, sizeof(int)); data_receive(infd, &task, tasksize*sizeof(Item)); }
/* * Bucket-sort algorithm. */ extern void bucketsort(int *array, int n) { int max; /* Maximum number. */ int i, j; /* Loop indexes. */ int range; /* Bucket range. */ struct minibucket *minib; /* Working mini-bucket. */ struct message *msg; /* Working message. */ struct bucket **todo; /* Todo buckets. */ struct bucket **done; /* Done buckets. */ uint64_t start, end; /* Timers. */ /* Setup slaves. */ open_noc_connectors(); spawn_slaves(); sync_slaves(); todo = smalloc(NUM_BUCKETS*sizeof(struct bucket *)); done = smalloc(NUM_BUCKETS*sizeof(struct bucket *)); for (i = 0; i < NUM_BUCKETS; i++) { done[i] = bucket_create(); todo[i] = bucket_create(); } /* Find max number in the array. */ start = timer_get(); max = INT_MIN; for (i = 0; i < n; i++) { /* Found. */ if (array[i] > max) max = array[i]; } /* Distribute numbers. */ range = max/NUM_BUCKETS; for (i = 0; i < n; i++) { j = array[i]/range; if (j >= NUM_BUCKETS) j = NUM_BUCKETS - 1; bucket_insert(&todo[j], array[i]); } end = timer_get(); master += timer_diff(start, end); /* Sort buckets. */ j = 0; for (i = 0; i < NUM_BUCKETS; i++) { while (bucket_size(todo[i]) > 0) { minib = bucket_pop(todo[i]); /* Send message. */ msg = message_create(SORTWORK, i, minib->size); message_send(outfd[j], msg); message_destroy(msg); /* Send data. */ communication += data_send(outfd[j], minib->elements, minib->size*sizeof(int)); minibucket_destroy(minib); j++; /* * Slave processes are busy. * So let's wait for results. */ if (j == nclusters) { /* Receive results. */ for (/* NOOP */ ; j > 0; j--) { /* Receive message. */ msg = message_receive(infd[nclusters - j]); /* Receive mini-bucket. */ minib = minibucket_create(); minib->size = msg->u.sortresult.size; communication += data_receive(infd[nclusters -j], minib->elements, minib->size*sizeof(int)); bucket_push(done[msg->u.sortresult.id], minib); message_destroy(msg); } } } } /* Receive results. */ for (/* NOOP */ ; j > 0; j--) { /* Receive message. */ msg = message_receive(infd[j - 1]); /* Receive bucket. */ minib = minibucket_create(); minib->size = msg->u.sortresult.size; communication += data_receive(infd[j - 1], minib->elements, minib->size*sizeof(int)); bucket_push(done[msg->u.sortresult.id], minib); message_destroy(msg); } start = timer_get(); rebuild_array(done, array); end = timer_get(); master += timer_diff(start, end); /* House keeping. */ for (i = 0; i < NUM_BUCKETS; i++) { bucket_destroy(todo[i]); bucket_destroy(done[i]); } free(done); free(todo); join_slaves(); close_noc_connectors(); }