int main(int argc, char **argv){ if(data_size * 8 > 4096){ coarray_init(2*4*data_size, argc, argv); } else { coarray_init(4096,argc, argv); } local_array<int> gather_result(num_images() * data_size); coarray<int, 1> source(dims{data_size});//parenthesis do not work here coarray<int, 1> scatter_result(dims{data_size}); for( int i = 0; i < data_size; i++ ) { source[i] = data_size * this_image() + i; } print(source); gather(gather_result, source); if(this_image() == 0) { cout << "Gather result:" << endl; for(int i = 0; i < num_images() * data_size; i++) { cout << gather_result[i] << ", "; gather_result[i]++;// = gather_result[i] + 1; } cout << endl; scatter(gather_result, scatter_result); } sync_all(); for(int i = 0; i < num_images(); i++) { if(this_image() != i) { cout << "scatter result(" << this_image() << "): "; for(int i = 0; i < data_size; i++) cout << scatter_result[i] << ", "; cout << endl; } } sync_all(); if(this_image() == 0) { collect(gather_result, scatter_result); cout << "collect result:" << endl; for(int i = 0; i < data_size; i++) cout << scatter_result[i] << ", "; cout << endl; } }
// done (BUSY) static int prepare_and_sync(csiebox_client* client) { char* cwd = (char*)malloc(sizeof(char) * PATH_MAX); memset(cwd, 0, sizeof(cwd)); if (getcwd(cwd, PATH_MAX) == 0) { fprintf(stderr, "getcwd fail\n"); fprintf(stderr, "code: %s\n", strerror(errno)); free(cwd); return 0; } if (chdir(client->arg.path) != 0) { fprintf(stderr, "invalid client path\n"); free(cwd); return 0; } max_level = 0; char* longest_path = (char*)malloc(sizeof(char) * PATH_MAX); sync_all(client, longest_path, 0); FILE *fp = fopen("longestPath.txt", "w+"); int i = 0, len = strlen(longest_path); for (; i<len-1; i++) { longest_path[i] = longest_path[i+1]; } longest_path[len-1] = 0; fwrite(longest_path, 1, strlen(longest_path), fp); fclose(fp); free(longest_path); // prepare message csiebox_protocol_header header; memset(&header, 0, sizeof(header)); header.req.magic = CSIEBOX_PROTOCOL_MAGIC_REQ; header.req.op = CSIEBOX_PROTOCOL_OP_SYNC_END; header.req.client_id = client->client_id; // send message check if is busy int request = 1; csiebox_protocol_header busy; while (1) { send_message(client->conn_fd, &request, sizeof(int)); recv_message(client->conn_fd, &busy, sizeof(header)); if (busy.res.status == CSIEBOX_PROTOCOL_STATUS_BUSY) { fprintf(stderr, "server busy\n"); sleep(3); } else break; } // start sync end send_message(client->conn_fd, &header, sizeof(header)); csiebox_protocol_header recv_header; memset(&recv_header, 0, sizeof(recv_header)); recv_message(client->conn_fd, &recv_header, sizeof(recv_header)); switch (recv_header.res.status) { case CSIEBOX_PROTOCOL_STATUS_OK: chdir(cwd); free(cwd); return 1; default: fprintf(stderr, "unknown status %x\n", recv_header.res.status); return 0; } return 0; }
/* void test1() { int id = this_image(); int team_size = num_images(); int extents[1]; extents[0] = team_size; coarray<int,1> test(dims(extents)); for(int i = 0; i < team_size; i++) { test( (id+i) % team_size)[id] = id ; } sync_all(); if(id == 0) { for( int i = 0; i < team_size; i++) { for( int j = 0; j < team_size; j++) { cout << test(i)[j] << ", "; } cout << endl; } } sync_all(); if(0 == id) { cout << endl << "\nTesting to see if the assignments to local corefs work:" << endl; test(0)[0] = 20; } sync_all(); if(0 == id) { cout << "\ttest(0)[0] from image 0: " << test(0)[0] << " (should be 20)" << endl; cout << "\ttest[0] from image 0: " << test[0] << " (should be 20)" << endl; } sync_all(); if(1 == id) { cout << "\ttest(0)[0] from image 1: " << test(0)[0] << " (should be 20)" << endl; } sync_all(); if(0 == id) { cout << endl << "\nTesting to see if the assignments to remote corefs work:" << endl; test(1)[0] = 42; } sync_all(); if(1 == id) { cout << "\ttest[0] from image 1: " << test[0] << " (should be 42)" << endl; } sync_all(); if(0 == id) { cout << "\nTesting remote coref assignment to local coref (without parenthesis)" << endl; test[0] = test(1)[0]; cout << "\ttest[0] from image 0: " << test[0] << " (should be 42)" << endl; } sync_all(); if(1 == id) { cout << "\ttest(0)[0] from image 1: " << test(0)[0] << " (should be 42)" << endl; } sync_all(); if(0 == id) { cout << "\nTesting remote coref + int assignment to local coref" << endl; test[1] = test(1)[0] + 1; } sync_all(); for(int i = 0; i < 3; i++){ if(id == i ) { cout << "\ttest(0)[1] from image " << i << ": " << test(0)[1] << " (should be 43)" << endl; } sync_all(); } if(0 == id) { cout << "\nTesting remote coref to another remote coref" << endl; test(2)[0] = test(1)[0]; } sync_all(); for(int i = 0; i < 3; i++){ if(id == i ) { cout << "\ttest(2)[0] from image " << i << ": " << test(2)[0] << " (should be 42)" << endl; } sync_all(); } if(0 == id) { cout << "\nTesting remote coref + int assignment to another remote coref" << endl; test(2)[1] = test(1)[0] + 1; } sync_all(); for(int i = 0; i < 3; i++){ if(id == i ) { cout << "\ttest(2)[1] from image " << i << ": " << test(2)[1] << " (should be 43)" << endl; } sync_all(); } if(0 == id) { cout << "\nTesting remote coref assignment to local int" << endl; int tmp = test(1)[0]; cout << "tmp should = 42: " << tmp << endl; } } void test2() { int id = this_image(); int team_size = num_images(); int extents[2] = {3, 5}; coarray<int,2> test2D(extents); //int counter = 0; for(int i = 0; i < extents[0]; i++) { for(int j = 0; j < extents[1]; j++) { //test2D[i][j] = counter; //counter++; if(id < extents[0]){ test2D( (id+i) % team_size)[id][j] = j + id; } } } if(id == 0) { for(int i = 0; i < extents[0]; i++) { for(int j = 0; j < extents[1]; j++) { cout << test2D[i][j] << ", "; } cout << endl; } for(int i = 0; i < extents[1] * extents[0]; i++) { cout << test2D[0][i] << ", "; } cout << endl; } sync_all(); int extent2[] = {2,4}; coarray<int,2> ca2(extent2); ca2[0][0] = 1; ca2[0][1] = 3; ca2[1][0] = 5; ca2[1][1] = 7; sync_all(); if(id == 0) { cout << "ca2 = [[" << ca2[0][0] << ", " << ca2[0][1] << "], [" << ca2[1][0] << ", " << ca2[1][1] << "]]" << endl; } sync_all(); for( int i = 0; i < team_size; i++) { if(i == id) { for( int j = 0; j < extents[0]; j++) { for( int k = 0; k < extents[1]; k++) { cout << test2D[j][k] << ", "; } cout << endl; } cout << endl; } sync_all(); } } void test3() { int id = this_image(); int team_size = num_images(); int extents[] = {4,4}; coarray<int,2> A(dims{4,4}, codims{2,2}); coarray<int,2> B(dims{4,4}, codims{2,2}); for(int i = 0; i < extents[1]; i++) { A[0][i] = i; B[0][i] = i + extents[0]; A[1][i] = -1; B[1][i] = -2; } cout << "A[0] before: "; for(int i = 0; i < extents[1]; i++) { cout << A[0][i] << ", "; } cout << endl; cout << "B[0] before: "; for(int i = 0; i < extents[1]; i++) { cout << B[0][i] << ", "; } cout << endl; A[1] = B[0]; B[1] = A[0]; cout << "A[0] after : "; for(int i = 0; i < extents[1]; i++) { cout << A[0][i] << ", "; } cout << endl; cout << "A[1] after : "; for(int i = 0; i < extents[1]; i++) { cout << A[1][i] << ", "; } cout << endl; cout << "B[0] after : "; for(int i = 0; i < extents[1]; i++) { cout << B[0][i] << ", "; } cout << endl; cout << "B[1] after : "; for(int i = 0; i < extents[1]; i++) { cout << B[1][i] << ", "; } cout << endl; cout << "A: " << endl; for(int i =0; i < 5; i++) { A[i].print(); } cout << "B: " << endl; for(int i =0; i < 3; i++) { B[i].print(); } cout << "C: " << endl; for(int i =0; i < 5; i++) { C[i].print(); } } void test4() { coarray<int,2> A(dims{4,4}, codims{1,2}); coarray<int,2> B(dims{4,4}, codims{1,2}); if(this_image() == 0) { A(0,0)[0][0] = 1; A(0,1)[0][0] = 2; A(1,0)[0][0] = 3; A(1,1)[0][0] = 4; } sync_all(); for(int i = 0; i < 4; i++) { if(this_image() == i) { cout << "A[0][0] = " << A[0][0] << endl; } sync_all(); } if(this_image() == 0) { cout << "getting A(0,1)[0]" << endl; A[1] = A(0,1)[0]; cout << "getting A(1,0)[0]" << endl; A[2] = A(1,0)[0]; cout << "getting A(1,1)[0]" << endl; A[3] = A(1,1)[0]; } sync_all(); if(this_image() == 0) { for(int i = 0; i < 4; i++) { cout << "A[" << i << "] :" << endl; for(int j = 0; j < 3; j++) { cout << A[i][j] << ", "; } cout << A[i][3] << endl; } } } void test5() { coarray<int,2> A(dims{4,4}, codims{1,2}); cout << "GASNET_PAGESIZE = " << GASNET_PAGESIZE << endl; cout << "Max local segment size: " << gasnet_getMaxLocalSegmentSize() << endl; int *b; b = A(0); A(0)[0][0] = 42; for(int i =0; i < num_images(); i++) { sync_all(); if(this_image() == i) { cout << "From image " << i << ", A(0)[0][0] = " << A(0)[0][0] << endl; } } } */ int main(int argc, char **argv) { coarray_init(128*1024, argc, argv); int id = this_image(); int team_size = num_images(); sync_all(); coarray<int,2> A(dims{4,4}, codims{1,2}); int *image_list = new int[num_images()-1]; int j = 0; for(int i = 0; i < num_images(); i++) { if(i != this_image()) { image_list[j] = i; j++; } } A[0][0] = 42 + this_image(); sync_images(image_list, num_images() - 1); if(this_image() == 0) { for(int i = 0; i < num_images(); i++) { cout << "A(" << i << ")[0][0] = " << A(i)[0][0] << endl; } } sync_all(); for(int i = 0; i < num_images(); i++) { if(this_image() == i) { cout << "on image " << i << ", A[0][0] = " << A[0][0] << endl; } sync_all(); } coarray_exit(); return 1; }
TPool::~TPool () { sync_all(); for ( unsigned int i = 0; i < _max_parallel; i++ ) _threads[i]->quit(); for ( unsigned int i = 0; i < _max_parallel; i++ ) { _threads[i]->del_mutex().lock(); delete _threads[i]; } delete[] _threads; }
TThreadPool::~TThreadPool () { // wait till all threads have finished sync_all(); // finish all thread for ( uint i = 0; i < _max_parallel; i++ ) _threads[i]->quit(); // cancel and delete all threads (not really safe !) for ( uint i = 0; i < _max_parallel; i++ ) { _threads[i]->del_mutex().lock(); delete _threads[i]; }// for delete[] _threads; }
static int prepare_and_sync(csiebox_client* client) { char* cwd = (char*)malloc(sizeof(char) * PATH_MAX); memset(cwd, 0, sizeof(cwd)); if (getcwd(cwd, PATH_MAX) == 0) { fprintf(stderr, "getcwd fail\n"); fprintf(stderr, "code: %s\n", strerror(errno)); free(cwd); return 0; } if (chdir(client->arg.path) != 0) { fprintf(stderr, "invalid client path\n"); free(cwd); return 0; } max_level = 0; char* longest_path = (char*)malloc(sizeof(char) * PATH_MAX); sync_all(client, longest_path, 0); // create longestPath.txt FILE *fp = fopen("longestPath.txt", "w+"); int i = 0, len = strlen(longest_path); for (; i<len-1; i++) { longest_path[i] = longest_path[i+1]; } longest_path[len-1] = 0; fwrite(longest_path, 1, strlen(longest_path), fp); fclose(fp); free(longest_path); handle_inotify(client); // send sync end message csiebox_protocol_header header; memset(&header, 0, sizeof(header)); header.req.magic = CSIEBOX_PROTOCOL_MAGIC_REQ; header.req.op = CSIEBOX_PROTOCOL_OP_SYNC_END; header.req.client_id = client->client_id; send_message(client->conn_fd, &header, sizeof(header)); chdir(cwd); free(cwd); return 1; }
static void sync_all(csiebox_client* client, char* longest_path, int level) { char* cwd = (char*)malloc(sizeof(char) * PATH_MAX); memset(cwd, 0, sizeof(char) * PATH_MAX); if (getcwd(cwd, PATH_MAX) == 0) { fprintf(stderr, "getcwd fail\n"); } add_inotify(client, cwd); DIR* dir; struct dirent* file; struct stat file_stat; dir = opendir("."); while ((file = readdir(dir)) != NULL) { if (strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0) { continue; } lstat(file->d_name, &file_stat); sync_file(client, file->d_name); if ((file_stat.st_mode & S_IFMT) == S_IFDIR) { level++; if (level > max_level) { max_level = level; strcpy(longest_path, convert_to_relative_path(client, file->d_name)); } if (chdir(file->d_name) != 0) { fprintf(stderr, "bad dir %s\n", file->d_name); continue; } sync_all(client, longest_path, level); chdir(cwd); } } closedir(dir); free(cwd); return; }