int main(int argc, char *argv[]) { int i,j, create_files; int fd_A, fd_B, file_size; int * fdata_A, *fdata_B; int matrix_len, row_block_len; struct stat finfo_A, finfo_B; char const* fname_A, *fname_B; int ret; struct timespec begin, end; get_time (begin); srand( (unsigned)time( NULL ) ); // Make sure a filename is specified if (argv[1] == NULL) { dprintf("USAGE: %s [side of matrix] [size of Row block]\n", argv[0]); exit(1); } fname_A = "matrix_file_A.txt"; fname_B = "matrix_file_B.txt"; CHECK_ERROR ( (matrix_len = atoi(argv[1])) < 0); file_size = ((matrix_len*matrix_len))*sizeof(int); fprintf(stderr, "***** file size is %d\n", file_size); if(argv[2] == NULL) row_block_len = 1; else CHECK_ERROR ( (row_block_len = atoi(argv[2])) < 0); if(argv[3] != NULL) create_files = 1; else create_files = 0; printf("MatrixMult: Side of the matrix is %d\n", matrix_len); printf("MatrixMult: Row Block Len is %d\n", row_block_len); printf("MatrixMult: Running...\n"); /* If the matrix files do not exist, create them */ if(create_files) { dprintf("Creating files\n"); int value = 0; CHECK_ERROR((fd_A = open(fname_A,O_CREAT | O_RDWR,S_IRWXU)) < 0); CHECK_ERROR((fd_B = open(fname_B,O_CREAT | O_RDWR,S_IRWXU)) < 0); for(i=0;i<matrix_len;i++) { for(j=0;j<matrix_len;j++) { value = (rand())%11; ret = write(fd_A,&value,sizeof(int)); assert(ret == sizeof(int)); //dprintf("%d ",value); } //dprintf("\n"); } //dprintf("\n"); for(i=0;i<matrix_len;i++) { for(j=0;j<matrix_len;j++) { value = (rand())%11; ret = write(fd_B,&value,sizeof(int)); assert(ret == sizeof(int)); //dprintf("%d ",value); } //dprintf("\n"); } CHECK_ERROR(close(fd_A) < 0); CHECK_ERROR(close(fd_B) < 0); } // Read in the file CHECK_ERROR((fd_A = open(fname_A,O_RDONLY)) < 0); // Get the file info (for file length) CHECK_ERROR(fstat(fd_A, &finfo_A) < 0); #ifndef NO_MMAP #ifdef MMAP_POPULATE // Memory map the file CHECK_ERROR((fdata_A = (int*)mmap(0, finfo_A.st_size + 1, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd_A, 0)) == NULL); #else // Memory map the file CHECK_ERROR((fdata_A = (int*)mmap(0, finfo_A.st_size + 1, PROT_READ, MAP_PRIVATE, fd_A, 0)) == NULL); #endif #else int ret; fdata_A = (char *)malloc (file_size); CHECK_ERROR (fdata_A == NULL); ret = read (fd_A, fdata_A, file_size); CHECK_ERROR (ret != file_size); #endif // Read in the file CHECK_ERROR((fd_B = open(fname_B,O_RDONLY)) < 0); // Get the file info (for file length) CHECK_ERROR(fstat(fd_B, &finfo_B) < 0); #ifndef NO_MMAP #ifdef MMAP_POPULATE // Memory map the file CHECK_ERROR((fdata_B = (int*)mmap(0, finfo_B.st_size + 1, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd_B, 0)) == NULL); #else // Memory map the file CHECK_ERROR((fdata_B = (int*)mmap(0, finfo_B.st_size + 1, PROT_READ, MAP_PRIVATE, fd_B, 0)) == NULL); #endif #else fdata_B = (char *)malloc (file_size); CHECK_ERROR (fdata_B == NULL); ret = read (fd_B, fdata_B, file_size); CHECK_ERROR (ret != file_size); #endif int* output = (int*)calloc(matrix_len*matrix_len, sizeof(int)); fprintf(stderr, "***** data size is %ld\n", (intptr_t)file_size); printf("MatrixMult: Calling MapReduce Scheduler Matrix Multiplication\n"); get_time (end); print_time("initialize", begin, end); get_time (begin); std::vector<MatrixMulMR::keyval> result; MatrixMulMR mapReduce(fdata_A, fdata_B, matrix_len, output); mapReduce.run(result); get_time (end); print_time("library", begin, end); get_time (begin); int sum = 0; for(i=0;i<matrix_len*matrix_len;i++) { sum += output[i]; } printf ("MatrixMult: total sum is %d\n", sum); printf("MatrixMult: MapReduce Completed\n"); free(output); #ifndef NO_MMAP CHECK_ERROR(munmap(fdata_A, file_size + 1) < 0); #else free (fdata_A); #endif CHECK_ERROR(close(fd_A) < 0); #ifndef NO_MMAP CHECK_ERROR(munmap(fdata_B, file_size + 1) < 0); #else free (fdata_B); #endif CHECK_ERROR(close(fd_B) < 0); get_time (end); print_time("finalize", begin, end); return 0; }
int main(int argc, char *argv[]) { int fd; char * fdata; unsigned int disp_num; struct stat finfo; char * fname, * disp_num_str; struct timespec begin, end; get_time (begin); // Make sure a filename is specified if (argv[1] == NULL) { printf("USAGE: %s <filename> [Top # of results to display]\n", argv[0]); exit(1); } fname = argv[1]; disp_num_str = argv[2]; printf("Wordcount: Running...\n"); // Read in the file CHECK_ERROR((fd = open(fname, O_RDONLY)) < 0); // Get the file info (for file length) CHECK_ERROR(fstat(fd, &finfo) < 0); uint64_t r = 0; fdata = (char *)malloc (finfo.st_size); CHECK_ERROR (fdata == NULL); while(r < (uint64_t)finfo.st_size) r += pread (fd, fdata + r, finfo.st_size, r); CHECK_ERROR (r != (uint64_t)finfo.st_size); // Get the number of results to display CHECK_ERROR((disp_num = (disp_num_str == NULL) ? DEFAULT_DISP_NUM : atoi(disp_num_str)) <= 0); get_time (end); #ifdef TIMING print_time("initialize", begin, end); #endif printf("Wordcount: Calling MapReduce Scheduler Wordcount\n"); get_time (begin); std::vector<WordsMR::keyval> result; WordsMR mapReduce(fdata, finfo.st_size, 1024*1024); CHECK_ERROR( mapReduce.run(result) < 0); get_time (end); #ifdef TIMING print_time("library", begin, end); #endif printf("Wordcount: MapReduce Completed\n"); get_time (begin); unsigned int dn = std::min(disp_num, (unsigned int)result.size()); printf("\nWordcount: Results (TOP %d of %lu):\n", dn, result.size()); uint64_t total = 0; for (size_t i = 0; i < dn; i++) { printf("%15s - %lu\n", result[result.size()-1-i].key.data, result[result.size()-1-i].val); } for(size_t i = 0; i < result.size(); i++) { total += result[i].val; } printf("Total: %lu\n", total); free (fdata); CHECK_ERROR(close(fd) < 0); get_time (end); #ifdef TIMING print_time("finalize", begin, end); #endif return 0; }