/* Initialization */ int main (int argc, char** argv) { int j,k; srandom(SEED); char fname[50]; sprintf(fname,"MM_PAR.%dP.%d.txt",NUM_THREADS,ARRAY_SIZE); f = fopen(fname,"w+"); /*The number of threads */ fprintf(f,"Number of threads = %d\n",NUM_THREADS); /*The number of data points for each experiment */ for(j=0; j<NUM_DATA_POINTS; j++) { fprintf(f,"Array size = %d\n",ARRAY_SIZE); CHUNK_SIZE = ARRAY_SIZE/NUM_THREADS; fprintf(f,"Chunk size = %d\n",CHUNK_SIZE); /* The inner loop to average for each data point */ for(k=0;k<NUM_TRIALS; k++) { fprintf(f,"Trial = %d\n",k); N = init_array(); M = init_array(); C = init_array(); randomize_array(N); randomize_array(M); par_multiply(); if(PRINT_SOLUTION) print_array(C); free(N); free(M); free(C); } ARRAY_SIZE += ARRAY_SIZE; } fclose(f); pthread_exit(NULL); return 0; }
int main(int argc, char **argv) { int rank, nprocs, i; struct timeval start; struct timeval end; // Initialize the MPI, get the size and the rank. MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); int array_size = atoi(argv[1]); srand(time(0)); printf("Array Size,%d\n", array_size ); int *data_array ; data_array = (int *) malloc(array_size * sizeof(int)); if (rank == MASTER_RANK) { randomize_array(data_array, array_size) ; if (DEBUG) { print_array(data_array, array_size, rank) ; } printf(" ----------------- \n"); } // RUN THE MPI_MyBcast method gettimeofday(&start,NULL); for (i=0; i< NUMBER_OF_TRIALS; i++) { MPI_Bcast(data_array, array_size, MPI_INT, MASTER_RANK, MPI_COMM_WORLD); } gettimeofday(&end,NULL); // if (rank == MASTER_RANK) { printf("Process, %d, MPI_Bcast,%f\n", rank, get_time_diff(&start, &end)); // } // RUN THE MPI_MyBcast method gettimeofday(&start,NULL); for (i=0; i< NUMBER_OF_TRIALS; i++) { MPI_MyBcast(data_array, array_size, MASTER_RANK, MPI_COMM_WORLD); } gettimeofday(&end,NULL); // if (rank == MASTER_RANK) { printf("Process,%d, MPI_MyBcast,%f\n", rank, get_time_diff(&start, &end)); // } if (DEBUG) { print_array(data_array, array_size, rank) ; } MPI_Finalize(); return 0; }
void mem_calibrate(void) { int i, p; // Limits for interval_rand() int lr = 0, hr = 100; // No of loops to calibrate with. int calib_length = MEM_CALIBRATION_LOOPS; // Working set size used for calibration. int calib_wss = MEM_CALIBRATION_WSS; long long start_usec, end_usec; // No of loops needed to achieve loop_length long long calib_loops; /* * Initialize the memness_array. This is done in such a way that each structure * points to the next one in the array, which is chosen at random. Care is * taken to ensure that there are no loops created with these pointers. */ for (i = 0; i < MEMNESS_INT_ARRAY_SIZE; i++) { memness_array[i].num = interval_rand(lr, hr); memness_array[i].touched = 1; memness_array[i].next = NULL; } randomize_array(memness_array, MEMNESS_INT_ARRAY_SIZE); if (!WILEE_CALIBRATE) return; /* * Calibrate by checking how long it takes to run calib_length number of * loops on a calib_wss sized working set. */ gettimeofday(&pr, NULL); mem_inner_loop(calib_length, calib_wss, p); gettimeofday(&ne, NULL); time_per_memness_iteration = (float)(timeval_diff(&pr, &ne)) / (float)calib_length; calib_loops = loop_length / time_per_memness_iteration; // calib_loops = 0; gettimeofday(&pr, NULL); mem_inner_loop(calib_loops, calib_wss, p); gettimeofday(&ne, NULL); printf("------------------\n"); printf("Memory Calibration\n"); printf("------------------\n"); printf("Working set size used: %d\n", calib_wss); printf("Time per iteration: %f\n", time_per_memness_iteration); printf("Number of loops for 100%% Memory-ness: %lld\n", calib_loops); printf("Time for above loops: %ld\n", timeval_diff(&pr, &ne)); }
int main() { int array[SIZE]; randomize_array(array, SIZE, SEED); max_subarray expected, answer; expected = find_maximum_subarray(array, 0, CROSSOVER_POINT); printf("%d elements, 10000 times...\n", CROSSOVER_POINT); TIME(10000, "brute-force ", find_maximum_subarray_brute, CROSSOVER_POINT); TIME(10000, "divide-and-conquer", find_maximum_subarray, CROSSOVER_POINT); TIME(10000, "mixed ", find_maximum_subarray_mixed, CROSSOVER_POINT); printf("=============================\n"); expected = find_maximum_subarray(array, 0, SIZE); printf("%d elements, 1 time...\n", SIZE); TIME(1, "brute-force ", find_maximum_subarray_brute, SIZE); TIME(1, "divide-and-conquer", find_maximum_subarray, SIZE); TIME(1, "mixed ", find_maximum_subarray_mixed, SIZE); exit(0); }
/* Initialization */ int main (int argc, char** argv) { int trial, data_point, i, rank, size, index; time_t t0, t1; clock_t c0, c1; MPI_Status status; MPI_Datatype chunk; MPI_Datatype matrix; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); srandom(SEED); char fname[255]; if(rank == 0) { sprintf(fname,"%sMM_MPI.%dP.%d.txt",LOG_PATH,size,ARRAY_SIZE); f = fopen(fname,"a"); /*The number of threads */ fprintf(f,"Number of threads = %d\n",size); fflush(f); } /*The number of data points for each experiment */ for(data_point=0; data_point<NUM_DATA_POINTS; data_point++) { int square = ARRAY_SIZE*ARRAY_SIZE; CHUNK_SIZE = square/size; if(rank == 0) { fprintf(f,"Array size = %d\n",ARRAY_SIZE); fflush(f); fprintf(f,"Chunk size = %d\n",CHUNK_SIZE); fflush(f); } long *our_chunk; long *results; //Allocate the memory we are going to need for the data point if(!(our_chunk = (long *)malloc(CHUNK_SIZE*sizeof(long)))) { fprintf(stderr, "Our Chunk Malloc failed, insf memory\n"); exit(EXIT_FAILURE); } if(!(results = (long *)malloc(CHUNK_SIZE*sizeof(long)))) { fprintf(stderr, "Results Malloc failed, insf memory\n"); exit(EXIT_FAILURE); } MPI_Type_contiguous(CHUNK_SIZE, MPI_LONG, &chunk); MPI_Type_commit(&chunk); MPI_Type_contiguous(square, MPI_LONG, &matrix); MPI_Type_commit(&matrix); if(rank ==0) { N = init_array(); C = init_array(); } M = init_array(); /* The inner loop to average for each data point */ for(trial=0;trial<NUM_TRIALS; trial++) { if(rank == 0) { fprintf(f,"Trial = %d\n",trial); fflush(f); randomize_array(N); randomize_array(M); /*Start Timing*/ t0 = time(NULL); c0 = clock(); //Send the data to the recievers for(i=1; i<size; i++) { index = i*ARRAY_SIZE; MPI_Send(&N[index], 1, chunk, i, 1, MPI_COMM_WORLD); MPI_Send(&M[0], 1, matrix, i, 1, MPI_COMM_WORLD); } //Set our chunk to the head our_chunk = &N[0]; } //Receive our chunk and M if(rank != 0) { MPI_Recv(our_chunk, CHUNK_SIZE, MPI_LONG, 0, 1 ,MPI_COMM_WORLD, &status); MPI_Recv(M, square, MPI_LONG, 0, 1 ,MPI_COMM_WORLD, &status); } //Multiply our chunk and the matrix results = multiply(our_chunk); //Send our results back to the master if(rank != 0) { MPI_Send(&results[0], 1, chunk, 0, 1, MPI_COMM_WORLD); } else { memcpy(&C[0], results, CHUNK_SIZE*sizeof(long*)); } //Gather the results from the slaves if(rank == 0) { for(i=1; i<size; i++) { MPI_Recv(&C[i*CHUNK_SIZE],CHUNK_SIZE, MPI_LONG, i, 1, MPI_COMM_WORLD, &status); } } MPI_Barrier(MPI_COMM_WORLD); //Stop timing and report if(rank == 0) { t1 = time(NULL); c1 = clock(); fprintf (f,"Elapsed wall clock time: %lds\n", (long) (t1 - t0)); fprintf (f,"Elapsed CPU time: %f\n", (float) (c1 - c0)/CLOCKS_PER_SEC); fflush(f); if(PRINT_SOLUTION) { print_array(N); print_array(M); print_array(C); } } //End of Trial } //End of Data Point Free up the matrices and re-init if(rank == 0) { fprintf(f,"-------------------------------\n\n"); fflush(f); } if(rank ==0) { free(N); free(C); } free(M); free(results); if(rank !=0) { free(our_chunk); } ARRAY_SIZE += ARRAY_SIZE; } //End of experiment, close the file pointer and leave if(rank == 0) { fclose(f); } MPI_Barrier(MPI_COMM_WORLD); MPI_Type_free(&chunk); MPI_Type_free(&matrix); MPI_Finalize(); return 0; }
int main(int argc, char* argv[]) { of_codec_id_t codec_id; /* identifier of the codec to use */ of_session_t *ses = NULL; /* openfec codec instance identifier */ of_parameters_t *params = NULL; /* structure used to initialize the openfec session */ void** enc_symbols_tab = NULL; /* table containing pointers to the encoding (i.e. source + repair) symbols buffers */ UINT32 symb_sz_32 = SYMBOL_SIZE / 4; /* symbol size in units of 32 bit words */ UINT32 k; /* number of source symbols in the block */ UINT32 n; /* number of encoding symbols (i.e. source + repair) in the block */ UINT32 esi; /* Encoding Symbol ID, used to identify each encoding symbol */ UINT32 i; UINT32* rand_order = NULL; /* table used to determine a random transmission order. This randomization process * is essential for LDPC-Staircase optimal performance */ SOCKET so = INVALID_SOCKET; /* UDP socket for server => client communications */ char *pkt_with_fpi = NULL; /* buffer containing a fixed size packet plus a header consisting only of the FPI */ fec_oti_t fec_oti; /* FEC Object Transmission Information as sent to the client */ INT32 lost_after_index= -1; /* all the packets to send after this index are considered as lost during transmission */ SOCKADDR_IN dst_host; UINT32 ret = -1; if (argc == 1) { /* k value is ommited, so use default */ k = DEFAULT_K; } else { k = atoi(argv[1]); } n = (UINT32)floor((double)k / (double)CODE_RATE); /* Choose which codec is the most appropriate. If small enough, choose Reed-Solomon (with m=8), otherwise LDPC-Staircase. * Then finish the openfec session initialization accordingly */ if (n <= 255) { /* fill in the code specific part of the of_..._parameters_t structure */ of_rs_2_m_parameters_t *my_params; printf("\nInitialize a Reed-Solomon over GF(2^m) codec instance, (n, k)=(%u, %u)...\n", n, k); codec_id = OF_CODEC_REED_SOLOMON_GF_2_M_STABLE; if ((my_params = (of_rs_2_m_parameters_t *)calloc(1, sizeof(* my_params))) == NULL) { OF_PRINT_ERROR(("no memory for codec %d\n", codec_id)) ret = -1; goto end; } my_params->m = 8; params = (of_parameters_t *) my_params; } else { /* fill in the code specific part of the of_..._parameters_t structure */ of_ldpc_parameters_t *my_params; printf("\nInitialize an LDPC-Staircase codec instance, (n, k)=(%u, %u)...\n", n, k); codec_id = OF_CODEC_LDPC_STAIRCASE_STABLE; if ((my_params = (of_ldpc_parameters_t *)calloc(1, sizeof(* my_params))) == NULL) { OF_PRINT_ERROR(("no memory for codec %d\n", codec_id)) ret = -1; goto end; } my_params->prng_seed = rand(); my_params->N1 = 7; params = (of_parameters_t *) my_params; } params->nb_source_symbols = k; /* fill in the generic part of the of_parameters_t structure */ params->nb_repair_symbols = n - k; params->encoding_symbol_length = SYMBOL_SIZE; /* Open and initialize the openfec session now... */ if (ret = of_create_codec_instance(&ses, codec_id, OF_ENCODER, VERBOSITY) != OF_STATUS_OK) { OF_PRINT_ERROR(("of_create_codec_instance() failed\n")) ret = -1; goto end; } if (of_set_fec_parameters(ses, params) != OF_STATUS_OK) { OF_PRINT_ERROR(("of_set_fec_parameters() failed for codec_id %d\n", codec_id)) ret = -1; goto end; } /* Allocate and initialize our source symbols... * In case of a file transmission, the opposite takes place: the file is read and partitionned into a set of k source symbols. * At the end, it's just equivalent since there is a set of k source symbols that need to be sent reliably thanks to an FEC * encoding. */ printf("\nFilling source symbols...\n"); if ((enc_symbols_tab = (void**) calloc(n, sizeof(void*))) == NULL) { OF_PRINT_ERROR(("no memory (calloc failed for enc_symbols_tab, n=%u)\n", n)) ret = -1; goto end; } /* In order to detect corruption, the first symbol is filled with 0x1111..., the second with 0x2222..., etc. * NB: the 0x0 value is avoided since it is a neutral element in the target finite fields, i.e. it prevents the detection * of symbol corruption */ for (esi = 0; esi < k; esi++ ) { if ((enc_symbols_tab[esi] = calloc(symb_sz_32, sizeof(UINT32))) == NULL) { OF_PRINT_ERROR(("no memory (calloc failed for enc_symbols_tab[%d])\n", esi)) ret = -1; goto end; } memset(enc_symbols_tab[esi], (char)(esi + 1), SYMBOL_SIZE); if (VERBOSITY > 1) { printf("src[%03d]= ", esi); dump_buffer_32(enc_symbols_tab[esi], 1); } } /* Now build the n-k repair symbols... */ printf("\nBuilding repair symbols...\n"); for (esi = k; esi < n; esi++) { if ((enc_symbols_tab[esi] = (char*)calloc(symb_sz_32, sizeof(UINT32))) == NULL) { OF_PRINT_ERROR(("no memory (calloc failed for enc_symbols_tab[%d])\n", esi)) ret = -1; goto end; } if (of_build_repair_symbol(ses, enc_symbols_tab, esi) != OF_STATUS_OK) { OF_PRINT_ERROR(("ERROR: of_build_repair_symbol() failed for esi=%u\n", esi)) ret = -1; goto end; } if (VERBOSITY > 1) { printf("repair[%03d]= ", esi); dump_buffer_32(enc_symbols_tab[esi], 4); } } /* Randomize the packet order, it's important for LDPC-Staircase codes for instance... */ printf("\nRandomizing transmit order...\n"); if ((rand_order = (UINT32*)calloc(n, sizeof(UINT32))) == NULL) { OF_PRINT_ERROR(("no memory (calloc failed for rand_order)\n")) ret = -1; goto end; } randomize_array(&rand_order, n); /* Finally initialize the UDP socket and throw our packets... */ if ((so = init_socket(&dst_host)) == INVALID_SOCKET) { OF_PRINT_ERROR(("Error initializing socket!\n")) ret = -1; goto end; } printf("First of all, send the FEC OTI for this object to %s/%d\n", DEST_IP, DEST_PORT); /* Initialize and send the FEC OTI to the client */ /* convert back to host endianess */ fec_oti.codec_id = htonl(codec_id); fec_oti.k = htonl(k); fec_oti.n = htonl(n); if ((ret = sendto(so, (void*)&fec_oti, sizeof(fec_oti), 0, (SOCKADDR *)&dst_host, sizeof(dst_host))) != sizeof(fec_oti)) { OF_PRINT_ERROR(("Error while sending the FEC OTI\n")) ret = -1; goto end; } lost_after_index = n * (1 - LOSS_RATE); if (lost_after_index < k) { OF_PRINT_ERROR(("The loss rate %f is to high: only %u packets will be sent, whereas k=%u\n", LOSS_RATE, lost_after_index, k)) ret = -1; goto end; } printf("Sending %u source and repair packets to %s/%d. All packets sent at index %u and higher are considered as lost\n", n, DEST_IP, DEST_PORT, lost_after_index); /* Allocate a buffer where we'll copy each symbol plus its simplistif FPI (in this example consisting only of the ESI). * This needs to be fixed in real applications, with the actual FPI required for this code. Also doing a memcpy is * rather suboptimal in terms of performance! */ if ((pkt_with_fpi = malloc(4 + SYMBOL_SIZE)) == NULL) { OF_PRINT_ERROR(("no memory (malloc failed for pkt_with_fpi)\n")) ret = -1; goto end; } for (i = 0; i < n; i++) { if (i == lost_after_index) { /* the remaining packets are considered as lost, exit loop */ break; } /* Add a pkt header wich only countains the ESI, i.e. a 32bits sequence number, in network byte order in order * to be portable regardless of the local and remote byte endian representation (the receiver will do the * opposite with ntohl()...) */ *(UINT32*)pkt_with_fpi = htonl(rand_order[i]); memcpy(4 + pkt_with_fpi, enc_symbols_tab[rand_order[i]], SYMBOL_SIZE); printf("%05d => sending symbol %u (%s)\n", i + 1, rand_order[i], (rand_order[i] < k) ? "src" : "repair"); if ((ret = sendto(so, pkt_with_fpi, SYMBOL_SIZE + 4, 0, (SOCKADDR *)&dst_host, sizeof(dst_host))) == SOCKET_ERROR) { OF_PRINT_ERROR(("sendto() failed!\n")) ret = -1; goto end; } } printf( "\nCompleted! %d packets sent successfully.\n", i); ret = 1; end: /* Cleanup everything... */ if (so!= INVALID_SOCKET) { close(so); } if (ses) { of_release_codec_instance(ses); } if (params) { free(params); } if (rand_order) { free(rand_order); } if (enc_symbols_tab) { for (esi = 0; esi < n; esi++) { if (enc_symbols_tab[esi]) { free(enc_symbols_tab[esi]); } } free(enc_symbols_tab); } return ret; }