int* multiMerge(int *a[], int size, int n) { if (n > 2) { return pairMerge(multiMerge(a, size / 2, n / 2), multiMerge(a + n/2, size / 2, n / 2), size); } else if (n == 1) { return a[0]; } return pairMerge(a[0], a[1], size); }
int main(int argc, char *argv[]) { struct timeval start_time, end_time; FILE *inp; char *s = strrchr(argv[2], '/'); char *filename; if (!s) { filename = strdup(argv[2]); // printf("%s\n", filename); } else { filename = strdup(s + 1); // printf("%s\n", filename); // return 0; } if( strcmp( argv[1], "--basic") == 0 ) { gettimeofday( &start_time, NULL ); // inp = fopen(argv[2], "r"); basicMerge(filename, argv[3]); // fclose(inp); gettimeofday( &end_time, NULL ); printf("Time: %lf\n", (end_time.tv_sec - start_time.tv_sec)+(end_time.tv_usec-start_time.tv_usec)/1000000.0); } else if( strcmp( argv[1], "--multistep") == 0 ) { gettimeofday( &start_time, NULL ); // inp = fopen(argv[2], "r"); multiMerge(filename, argv[3]); // fclose(inp); gettimeofday( &end_time, NULL ); printf("Time: %lf\n", (end_time.tv_sec - start_time.tv_sec)+(end_time.tv_usec-start_time.tv_usec)/1000000.0); } else if( strcmp( argv[1], "--replacement") == 0 ) { gettimeofday( &start_time, NULL ); // inp = fopen(argv[2], "r"); replacementMerge(filename, argv[3]); // fclose(inp); gettimeofday( &end_time, NULL ); printf("Time: %lf\n", (end_time.tv_sec - start_time.tv_sec)+(end_time.tv_usec-start_time.tv_usec)/1000000.0); } else { printf("Wrong argument\n"); exit(1); } return 0; }
int main(int argc, char** argv) { int i, n; int* A, *temp; clock_t start, end; double elapsed_time, t1, t2; MPI_Init(NULL, NULL); int world_rank, world_size; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Comm_size(MPI_COMM_WORLD, &world_size); n = world_size; int chunkSize = N / world_size; int start_index = world_rank * chunkSize; t1 = MPI_Wtime(); A = (int *)malloc(sizeof(int)*chunkSize); temp = (int *)malloc(sizeof(int)*N); if (A == NULL) { printf("Fail to malloc\n"); exit(1); } for (i=chunkSize-1; i>=0; i--) A[chunkSize-1-i] = i + start_index; if (isSorted(A, chunkSize)) printf("Array is sorted\n"); else printf("Array is NOT sorted\n"); bubbleSort(A, chunkSize); if (world_rank == 0) { int rank; int *partial[n]; printArray(A, 5); partial[0] = A; for (rank = 1; rank < world_size; rank++) { int* rev = (int *)malloc(sizeof(int)*chunkSize); MPI_Recv(rev, chunkSize, MPI_INT, rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printArray(rev, 5); partial[rank] = rev; } temp = multiMerge(partial, N, n); if (isSorted(temp, N)) printf("Array is sorted\n"); else printf("Array is NOT sorted\n"); t2 = MPI_Wtime(); printf( "Elapsed time MPI_Wtime is %f\n", t2 - t1 ); //printArray(temp, N); } else if (world_rank != 0) { MPI_Send(A, chunkSize, MPI_INT, 0, 0, MPI_COMM_WORLD); } MPI_Finalize(); return 0; }