FORT_DLL_SPEC void FORT_CALL mpi_file_set_size_ ( MPI_Fint *v1, MPI_Offset *v2, MPI_Fint *ierr ){ #ifdef MPI_MODE_RDONLY *ierr = MPI_File_set_size( MPI_File_f2c(*v1), (MPI_Offset)*v2 ); #else *ierr = MPI_ERR_INTERN; #endif }
FORTRAN_API void FORT_CALL mpi_file_set_size_(MPI_Fint *fh,MPI_Offset *size, MPI_Fint *ierr ) { MPI_File fh_c; fh_c = MPI_File_f2c(*fh); *ierr = MPI_File_set_size(fh_c,*size); }
static int destroyAFiledataMPINONB(void *v) { int iret = 0; aFiledataM *of; MPI_Status status; int rankNode = commInqRankNode (); MPI_Offset endpos; of = (aFiledataM * ) v; xdebug ( "IOPE%d: close file %d, name=\"%s\"", rankNode, of->fileID, of->name ); /* close file */ xmpi(MPI_Wait(&of->request, &status)); xmpi(MPI_Barrier(commInqCommNode())); xmpi(MPI_File_get_position_shared(of->fh, &endpos)); xmpi(MPI_File_set_size(of->fh, endpos)); iret = MPI_File_close ( & ( of->fh )); /* file closed, cleanup */ dbuffer_cleanup ( & ( of->db1 )); dbuffer_cleanup ( & ( of->db2 )); free ( of ); xdebug ( "IOPE%d: closed file, cleaned up, return", rankNode ); return iret == MPI_SUCCESS ? 0 : -1; }
JNIEXPORT void JNICALL Java_mpi_File_setSize( JNIEnv *env, jobject jthis, jlong fh, jlong size) { int rc = MPI_File_set_size((MPI_File)fh, (MPI_Offset)size); ompi_java_exceptionCheck(env, rc); }
int main(int argc, char** argv) { MPI_Init(&argc, &argv); setup_globals(); /* Parse arguments. */ int SCALE = 16; int edgefactor = 16; /* nedges / nvertices, i.e., 2*avg. degree */ // if (argc >= 2) SCALE = atoi(argv[1]); // if (argc >= 3) edgefactor = atoi(argv[2]); char* name = argv[1]; if (argc >= 3) SCALE = atoi(argv[2]); if (argc >= 4) edgefactor = atoi(argv[3]); // if (argc <= 1 || argc >= 4 || SCALE == 0 || edgefactor == 0) { // if (rank == 0) { // fprintf(stderr, "Usage: %s SCALE edgefactor\n SCALE = log_2(# vertices) [integer, required]\n edgefactor = (# edges) / (# vertices) = .5 * (average vertex degree) [integer, defaults to 16]\n(Random number seed and Kronecker initiator are in main.c)\n", argv[0]); // } if (argc <= 2 || argc >= 5 || SCALE == 0 || edgefactor == 0) { if (rank == 0) { fprintf(stderr, "Usage: %s filename SCALE edgefactor\n SCALE = log_2(# vertices) [integer, required]\n edgefactor = (# edges) / (# vertices) = .5 * (average vertex degree) [integer, defaults to 16]\n(Random number seed and Kronecker initiator are in main.c)\n", argv[0]); } MPI_Abort(MPI_COMM_WORLD, 1); } uint64_t seed1 = 2, seed2 = 3; // const char* filename = getenv("TMPFILE"); const char* filename = name; /* If filename is NULL, store data in memory */ tuple_graph tg; tg.nglobaledges = (int64_t)(edgefactor) << SCALE; int64_t nglobalverts = (int64_t)(1) << SCALE; tg.data_in_file = (filename != NULL); if (tg.data_in_file) { printf("data in file \n"); MPI_File_set_errhandler(MPI_FILE_NULL, MPI_ERRORS_ARE_FATAL); // MPI_File_open(MPI_COMM_WORLD, (char*)filename, MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_EXCL | MPI_MODE_DELETE_ON_CLOSE | MPI_MODE_UNIQUE_OPEN, MPI_INFO_NULL, &tg.edgefile); MPI_File_open(MPI_COMM_WORLD, (char*)filename, MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_EXCL | MPI_MODE_UNIQUE_OPEN, MPI_INFO_NULL, &tg.edgefile); MPI_File_set_size(tg.edgefile, tg.nglobaledges * sizeof(packed_edge)); MPI_File_set_view(tg.edgefile, 0, packed_edge_mpi_type, packed_edge_mpi_type, "native", MPI_INFO_NULL); MPI_File_set_atomicity(tg.edgefile, 0); } /* Make the raw graph edges. */ /* Get roots for BFS runs, plus maximum vertex with non-zero degree (used by * validator). */ int num_bfs_roots = 64; int64_t* bfs_roots = (int64_t*)xmalloc(num_bfs_roots * sizeof(int64_t)); int64_t max_used_vertex = 0; double make_graph_start = MPI_Wtime(); { /* Spread the two 64-bit numbers into five nonzero values in the correct * range. */ uint_fast32_t seed[5]; make_mrg_seed(seed1, seed2, seed); /* As the graph is being generated, also keep a bitmap of vertices with * incident edges. We keep a grid of processes, each row of which has a * separate copy of the bitmap (distributed among the processes in the * row), and then do an allreduce at the end. This scheme is used to avoid * non-local communication and reading the file separately just to find BFS * roots. */ MPI_Offset nchunks_in_file = (tg.nglobaledges + FILE_CHUNKSIZE - 1) / FILE_CHUNKSIZE; int64_t bitmap_size_in_bytes = int64_min(BITMAPSIZE, (nglobalverts + CHAR_BIT - 1) / CHAR_BIT); if (bitmap_size_in_bytes * size * CHAR_BIT < nglobalverts) { bitmap_size_in_bytes = (nglobalverts + size * CHAR_BIT - 1) / (size * CHAR_BIT); } int ranks_per_row = ((nglobalverts + CHAR_BIT - 1) / CHAR_BIT + bitmap_size_in_bytes - 1) / bitmap_size_in_bytes; int nrows = size / ranks_per_row; int my_row = -1, my_col = -1; unsigned char* restrict has_edge = NULL; MPI_Comm cart_comm; { int dims[2] = {size / ranks_per_row, ranks_per_row}; int periods[2] = {0, 0}; MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 1, &cart_comm); } int in_generating_rectangle = 0; if (cart_comm != MPI_COMM_NULL) { in_generating_rectangle = 1; { int dims[2], periods[2], coords[2]; MPI_Cart_get(cart_comm, 2, dims, periods, coords); my_row = coords[0]; my_col = coords[1]; } MPI_Comm this_col; MPI_Comm_split(cart_comm, my_col, my_row, &this_col); MPI_Comm_free(&cart_comm); has_edge = (unsigned char*)xMPI_Alloc_mem(bitmap_size_in_bytes); memset(has_edge, 0, bitmap_size_in_bytes); /* Every rank in a given row creates the same vertices (for updating the * bitmap); only one writes them to the file (or final memory buffer). */ packed_edge* buf = (packed_edge*)xmalloc(FILE_CHUNKSIZE * sizeof(packed_edge)); MPI_Offset block_limit = (nchunks_in_file + nrows - 1) / nrows; // fprintf(stderr, "%d: nchunks_in_file = %" PRId64 ", block_limit = %" PRId64 " in grid of %d rows, %d cols\n", rank, (int64_t)nchunks_in_file, (int64_t)block_limit, nrows, ranks_per_row); if (tg.data_in_file) { tg.edgememory_size = 0; tg.edgememory = NULL; } else { int my_pos = my_row + my_col * nrows; int last_pos = (tg.nglobaledges % ((int64_t)FILE_CHUNKSIZE * nrows * ranks_per_row) != 0) ? (tg.nglobaledges / FILE_CHUNKSIZE) % (nrows * ranks_per_row) : -1; int64_t edges_left = tg.nglobaledges % FILE_CHUNKSIZE; int64_t nedges = FILE_CHUNKSIZE * (tg.nglobaledges / ((int64_t)FILE_CHUNKSIZE * nrows * ranks_per_row)) + FILE_CHUNKSIZE * (my_pos < (tg.nglobaledges / FILE_CHUNKSIZE) % (nrows * ranks_per_row)) + (my_pos == last_pos ? edges_left : 0); /* fprintf(stderr, "%d: nedges = %" PRId64 " of %" PRId64 "\n", rank, (int64_t)nedges, (int64_t)tg.nglobaledges); */ tg.edgememory_size = nedges; tg.edgememory = (packed_edge*)xmalloc(nedges * sizeof(packed_edge)); } MPI_Offset block_idx; for (block_idx = 0; block_idx < block_limit; ++block_idx) { /* fprintf(stderr, "%d: On block %d of %d\n", rank, (int)block_idx, (int)block_limit); */ MPI_Offset start_edge_index = int64_min(FILE_CHUNKSIZE * (block_idx * nrows + my_row), tg.nglobaledges); MPI_Offset edge_count = int64_min(tg.nglobaledges - start_edge_index, FILE_CHUNKSIZE); packed_edge* actual_buf = (!tg.data_in_file && block_idx % ranks_per_row == my_col) ? tg.edgememory + FILE_CHUNKSIZE * (block_idx / ranks_per_row) : buf; /* fprintf(stderr, "%d: My range is [%" PRId64 ", %" PRId64 ") %swriting into index %" PRId64 "\n", rank, (int64_t)start_edge_index, (int64_t)(start_edge_index + edge_count), (my_col == (block_idx % ranks_per_row)) ? "" : "not ", (int64_t)(FILE_CHUNKSIZE * (block_idx / ranks_per_row))); */ if (!tg.data_in_file && block_idx % ranks_per_row == my_col) { assert (FILE_CHUNKSIZE * (block_idx / ranks_per_row) + edge_count <= tg.edgememory_size); } // debug char* wtxbuf = (char*)xmalloc(FILE_CHUNKSIZE * sizeof(packed_edge)); // generate_kronecker_range(seed, SCALE, start_edge_index, start_edge_index + edge_count, actual_buf); generate_kronecker_range(seed, SCALE, start_edge_index, start_edge_index + edge_count, actual_buf); if (tg.data_in_file && my_col == (block_idx % ranks_per_row)) { /* Try to spread writes among ranks */ // MPI_File_write_at(tg.edgefile, start_edge_index, actual_buf, edge_count, packed_edge_mpi_type, MPI_STATUS_IGNORE); // debug printf("%d: %d, %d\n", rank, start_edge_index, edge_count); int i; // for (i = start_edge_index; i < start_edge_index + 3; i++) { // if(block_idx == 0) { // for (i = 0; i < 3; i++) { // if (edge_count > 3) // printf("%d: %d\t%d\n", rank, actual_buf[i].v0, actual_buf[i].v1); // } // } MPI_File_write_at(tg.edgefile, start_edge_index, actual_buf, edge_count, packed_edge_mpi_type, MPI_STATUS_IGNORE); } ptrdiff_t i; #ifdef _OPENMP #pragma omp parallel for #endif for (i = 0; i < edge_count; ++i) { int64_t src = get_v0_from_edge(&actual_buf[i]); int64_t tgt = get_v1_from_edge(&actual_buf[i]); if (src == tgt) continue; if (src / bitmap_size_in_bytes / CHAR_BIT == my_col) { #ifdef _OPENMP #pragma omp atomic #endif has_edge[(src / CHAR_BIT) % bitmap_size_in_bytes] |= (1 << (src % CHAR_BIT)); } if (tgt / bitmap_size_in_bytes / CHAR_BIT == my_col) { #ifdef _OPENMP #pragma omp atomic #endif has_edge[(tgt / CHAR_BIT) % bitmap_size_in_bytes] |= (1 << (tgt % CHAR_BIT)); } } } free(buf); #if 0 /* The allreduce for each root acts like we did this: */ MPI_Allreduce(MPI_IN_PLACE, has_edge, bitmap_size_in_bytes, MPI_UNSIGNED_CHAR, MPI_BOR, this_col); #endif MPI_Comm_free(&this_col); } else { tg.edgememory = NULL; tg.edgememory_size = 0; } MPI_Allreduce(&tg.edgememory_size, &tg.max_edgememory_size, 1, MPI_INT64_T, MPI_MAX, MPI_COMM_WORLD); #ifndef GEN_ONLY /* Find roots and max used vertex */ { uint64_t counter = 0; int bfs_root_idx; for (bfs_root_idx = 0; bfs_root_idx < num_bfs_roots; ++bfs_root_idx) { int64_t root; while (1) { double d[2]; make_random_numbers(2, seed1, seed2, counter, d); root = (int64_t)((d[0] + d[1]) * nglobalverts) % nglobalverts; counter += 2; if (counter > 2 * nglobalverts) break; int is_duplicate = 0; int i; for (i = 0; i < bfs_root_idx; ++i) { if (root == bfs_roots[i]) { is_duplicate = 1; break; } } if (is_duplicate) continue; /* Everyone takes the same path here */ int root_ok = 0; if (in_generating_rectangle && (root / CHAR_BIT / bitmap_size_in_bytes) == my_col) { root_ok = (has_edge[(root / CHAR_BIT) % bitmap_size_in_bytes] & (1 << (root % CHAR_BIT))) != 0; } MPI_Allreduce(MPI_IN_PLACE, &root_ok, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD); if (root_ok) break; } bfs_roots[bfs_root_idx] = root; } num_bfs_roots = bfs_root_idx; /* Find maximum non-zero-degree vertex. */ { int64_t i; max_used_vertex = 0; if (in_generating_rectangle) { for (i = bitmap_size_in_bytes * CHAR_BIT; i > 0; --i) { if (i > nglobalverts) continue; if (has_edge[(i - 1) / CHAR_BIT] & (1 << ((i - 1) % CHAR_BIT))) { max_used_vertex = (i - 1) + my_col * CHAR_BIT * bitmap_size_in_bytes; break; } } } MPI_Allreduce(MPI_IN_PLACE, &max_used_vertex, 1, MPI_INT64_T, MPI_MAX, MPI_COMM_WORLD); } } #endif if (in_generating_rectangle) { MPI_Free_mem(has_edge); } if (tg.data_in_file) { MPI_File_sync(tg.edgefile); } } double make_graph_stop = MPI_Wtime(); double make_graph_time = make_graph_stop - make_graph_start; if (rank == 0) { /* Not an official part of the results */ fprintf(stderr, "graph_generation: %f s\n", make_graph_time); } //debug #ifndef GEN_ONLY //!GEN_ONLY /* Make user's graph data structure. */ double data_struct_start = MPI_Wtime(); make_graph_data_structure(&tg); double data_struct_stop = MPI_Wtime(); double data_struct_time = data_struct_stop - data_struct_start; if (rank == 0) { /* Not an official part of the results */ fprintf(stderr, "construction_time: %f s\n", data_struct_time); } /* Number of edges visited in each BFS; a double so get_statistics can be * used directly. */ double* edge_counts = (double*)xmalloc(num_bfs_roots * sizeof(double)); /* Run BFS. */ int validation_passed = 1; double* bfs_times = (double*)xmalloc(num_bfs_roots * sizeof(double)); double* validate_times = (double*)xmalloc(num_bfs_roots * sizeof(double)); uint64_t nlocalverts = get_nlocalverts_for_pred(); int64_t* pred = (int64_t*)xMPI_Alloc_mem(nlocalverts * sizeof(int64_t)); int bfs_root_idx; for (bfs_root_idx = 0; bfs_root_idx < num_bfs_roots; ++bfs_root_idx) { int64_t root = bfs_roots[bfs_root_idx]; if (rank == 0) fprintf(stderr, "Running BFS %d\n", bfs_root_idx); /* Clear the pred array. */ memset(pred, 0, nlocalverts * sizeof(int64_t)); /* Do the actual BFS. */ double bfs_start = MPI_Wtime(); run_bfs(root, &pred[0]); double bfs_stop = MPI_Wtime(); bfs_times[bfs_root_idx] = bfs_stop - bfs_start; if (rank == 0) fprintf(stderr, "Time for BFS %d is %f\n", bfs_root_idx, bfs_times[bfs_root_idx]); /* Validate result. */ if (rank == 0) fprintf(stderr, "Validating BFS %d\n", bfs_root_idx); double validate_start = MPI_Wtime(); int64_t edge_visit_count; int validation_passed_one = validate_bfs_result(&tg, max_used_vertex + 1, nlocalverts, root, pred, &edge_visit_count); double validate_stop = MPI_Wtime(); validate_times[bfs_root_idx] = validate_stop - validate_start; if (rank == 0) fprintf(stderr, "Validate time for BFS %d is %f\n", bfs_root_idx, validate_times[bfs_root_idx]); edge_counts[bfs_root_idx] = (double)edge_visit_count; if (rank == 0) fprintf(stderr, "TEPS for BFS %d is %g\n", bfs_root_idx, edge_visit_count / bfs_times[bfs_root_idx]); if (!validation_passed_one) { validation_passed = 0; if (rank == 0) fprintf(stderr, "Validation failed for this BFS root; skipping rest.\n"); break; } } MPI_Free_mem(pred); free(bfs_roots); free_graph_data_structure(); #endif //!GEN_ONLY if (tg.data_in_file) { MPI_File_close(&tg.edgefile); } else { free(tg.edgememory); tg.edgememory = NULL; } #ifndef GEN_ONLY /* Print results. */ if (rank == 0) { if (!validation_passed) { fprintf(stdout, "No results printed for invalid run.\n"); } else { int i; fprintf(stdout, "SCALE: %d\n", SCALE); fprintf(stdout, "edgefactor: %d\n", edgefactor); fprintf(stdout, "NBFS: %d\n", num_bfs_roots); fprintf(stdout, "graph_generation: %g\n", make_graph_time); fprintf(stdout, "num_mpi_processes: %d\n", size); fprintf(stdout, "construction_time: %g\n", data_struct_time); double stats[s_LAST]; get_statistics(bfs_times, num_bfs_roots, stats); fprintf(stdout, "min_time: %g\n", stats[s_minimum]); fprintf(stdout, "firstquartile_time: %g\n", stats[s_firstquartile]); fprintf(stdout, "median_time: %g\n", stats[s_median]); fprintf(stdout, "thirdquartile_time: %g\n", stats[s_thirdquartile]); fprintf(stdout, "max_time: %g\n", stats[s_maximum]); fprintf(stdout, "mean_time: %g\n", stats[s_mean]); fprintf(stdout, "stddev_time: %g\n", stats[s_std]); get_statistics(edge_counts, num_bfs_roots, stats); fprintf(stdout, "min_nedge: %.11g\n", stats[s_minimum]); fprintf(stdout, "firstquartile_nedge: %.11g\n", stats[s_firstquartile]); fprintf(stdout, "median_nedge: %.11g\n", stats[s_median]); fprintf(stdout, "thirdquartile_nedge: %.11g\n", stats[s_thirdquartile]); fprintf(stdout, "max_nedge: %.11g\n", stats[s_maximum]); fprintf(stdout, "mean_nedge: %.11g\n", stats[s_mean]); fprintf(stdout, "stddev_nedge: %.11g\n", stats[s_std]); double* secs_per_edge = (double*)xmalloc(num_bfs_roots * sizeof(double)); for (i = 0; i < num_bfs_roots; ++i) secs_per_edge[i] = bfs_times[i] / edge_counts[i]; get_statistics(secs_per_edge, num_bfs_roots, stats); fprintf(stdout, "min_TEPS: %g\n", 1. / stats[s_maximum]); fprintf(stdout, "firstquartile_TEPS: %g\n", 1. / stats[s_thirdquartile]); fprintf(stdout, "median_TEPS: %g\n", 1. / stats[s_median]); fprintf(stdout, "thirdquartile_TEPS: %g\n", 1. / stats[s_firstquartile]); fprintf(stdout, "max_TEPS: %g\n", 1. / stats[s_minimum]); fprintf(stdout, "harmonic_mean_TEPS: %g\n", 1. / stats[s_mean]); /* Formula from: * Title: The Standard Errors of the Geometric and Harmonic Means and * Their Application to Index Numbers * Author(s): Nilan Norris * Source: The Annals of Mathematical Statistics, Vol. 11, No. 4 (Dec., 1940), pp. 445-448 * Publisher(s): Institute of Mathematical Statistics * Stable URL: http://www.jstor.org/stable/2235723 * (same source as in specification). */ fprintf(stdout, "harmonic_stddev_TEPS: %g\n", stats[s_std] / (stats[s_mean] * stats[s_mean] * sqrt(num_bfs_roots - 1))); free(secs_per_edge); secs_per_edge = NULL; free(edge_counts); edge_counts = NULL; get_statistics(validate_times, num_bfs_roots, stats); fprintf(stdout, "min_validate: %g\n", stats[s_minimum]); fprintf(stdout, "firstquartile_validate: %g\n", stats[s_firstquartile]); fprintf(stdout, "median_validate: %g\n", stats[s_median]); fprintf(stdout, "thirdquartile_validate: %g\n", stats[s_thirdquartile]); fprintf(stdout, "max_validate: %g\n", stats[s_maximum]); fprintf(stdout, "mean_validate: %g\n", stats[s_mean]); fprintf(stdout, "stddev_validate: %g\n", stats[s_std]); #if 0 for (i = 0; i < num_bfs_roots; ++i) { fprintf(stdout, "Run %3d: %g s, validation %g s\n", i + 1, bfs_times[i], validate_times[i]); } #endif } } free(bfs_times); free(validate_times); #endif cleanup_globals(); MPI_Finalize(); return 0; }
int main( int argc, char *argv[] ) { int itr; int operacao; char * chave_file; char * entrada_file; char * saida_file; octeto Nb,Nk,Nr; octeto bloco[4*8]; octeto chave[4*8*15]; int worldsize, rank; MPI_Status status; MPI_File chave_handle; MPI_File entrada_handle; MPI_File saida_handle; MPI_Offset entrada_bytes; unsigned int numero_blocos; unsigned int blocos_processo; MPI_Offset bloco_byte_inicio; MPI_Offset bloco_byte_fim; MPI_Offset iterador; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&worldsize); MPI_Comm_rank(MPI_COMM_WORLD,&rank); operacao = INDEFINIDA; chave_file = NULL; entrada_file = NULL; saida_file = NULL; for(itr = 1;itr < argc;itr++) { /* Instrucoes de uso */ if( strcmp(argv[itr],"-a") == 0 || strcmp(argv[itr],"--ajuda") == 0 || strcmp(argv[itr],"-h") == 0 || strcmp(argv[itr],"--help") == 0 ) { if(rank == 0) { printf(" Uso: mpiexec -n [PROCESSOS] ./rijndael [ARGUMENTO VALOR].\n"); printf(" Encripta/Decripta um arquivo usando o algoritmo Rijndael(AES) extendido.\n"); printf(" Argumentos obrigatorios:\n"); printf(" -op,--operacao: Informa se o objetivo da execucao eh encriptar ou decriptar.\n"); printf(" * Os valores possiveis sao: \'encriptar\' e \'decriptar\'.\n"); printf(" -e,-i,--entrada,--input: Caminho e nome do arquivo a ser criptografado.\n"); printf(" -s,-o,--saida,--output: Caminho e nome do arquivo resultante do processo de criptografia da entrada.\n"); printf(" -c,-k,--chave,--key: Caminho e nome do arquivo contendo a chave.\n"); printf(" O arquivo contendo a chave eh em formato binario de acordo com a seguinte especificacao:\n"); printf(" - O primeiro byte deve conter o tamanho do bloco (em palavras de 4 bytes).\n"); printf(" * O bloco pode possuir tamanho: 4, 5, 6, 7 ou 8.\n"); printf(" - O segundo byte deve conter o tamanho da chave (em palavras de 4 bytes).\n"); printf(" * Esta aplicacao aceita chaves com tamanho: 4, 5, 6, 7 ou 8.\n"); printf(" - Os proximos 4*[tamanho da chave] bytes do arquivo sao os bytes componentes da chave, que\n"); printf(" devem estar (obrigatoriamente) escritos no formato hexadecimal da linguagem C (0xff).\n"); printf(" * Eh recomendavel o uso de um editor hexadecimal na construcao do arquivo chave.\n"); } goto finalizando; } /* Operacao a ser realizada */ else if( strcmp(argv[itr],"-op") == 0 || strcmp(argv[itr],"--operacao") == 0 ) { if( itr+1 < argc ) { if( strcmp(argv[itr+1],"encriptar") == 0 ) { operacao = ENCRIPTAR; } else if( strcmp(argv[itr+1],"decriptar") == 0 ) { operacao = DECRIPTAR; } itr++; } else { goto sempar; } } /* Arquivo com a chave */ else if( strcmp(argv[itr],"-c") == 0 || strcmp(argv[itr],"--chave") == 0 || strcmp(argv[itr],"-k") == 0 || strcmp(argv[itr],"--key") == 0 ) { if(itr+1 < argc) { chave_file = argv[itr+1]; itr++; } else { goto sempar; } } /* Arquivo de entrada */ else if( strcmp(argv[itr],"-e") == 0 || strcmp(argv[itr],"--entrada") == 0 || strcmp(argv[itr],"-i") == 0 || strcmp(argv[itr],"--input") == 0 ) { if(itr+1 < argc) { entrada_file = argv[itr+1]; itr++; } else { goto sempar; } } /* Arquivo de saida */ else if( strcmp(argv[itr],"-s") == 0 || strcmp(argv[itr],"--saida") == 0 || strcmp(argv[itr],"-o") == 0 || strcmp(argv[itr],"--output") == 0 ) { if(itr+1 < argc) { saida_file = argv[itr+1]; itr++; } else { goto sempar; } } /* Erro desconhecido */ else { if(rank == 0) { printf("Erro nos argumentos passados.\n"); } goto help; } } /* Fim da leitura dos argumentos */ if( operacao == INDEFINIDA || chave_file == NULL || entrada_file == NULL || saida_file == NULL ) { if(rank == 0) { if( operacao == INDEFINIDA ) printf("A operacao a ser realizada eh invalida ou nao foi especificada.\n"); if( chave_file == NULL ) printf("Esta faltando especificar o arquivo com a chave.\n"); if( entrada_file == NULL ) printf("Esta faltando especificar o arquivo de entrada.\n"); if( saida_file == NULL ) printf("Esta faltando especificar o arquivo de saida.\n"); } goto help; } /* Fim do tratamento dos argumentos */ if( MPI_File_open( MPI_COMM_WORLD, chave_file, MPI_MODE_RDONLY, MPI_INFO_NULL, &chave_handle ) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na abertura do arquivo com a chave (%s).\n",chave_file); } goto help; } if( MPI_File_read(chave_handle,&Nb,1, MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na leitura do tamanho de um bloco no arquivo com a chave (%s).\n",chave_file); } goto help; } if( Nb< 4 || Nb > 8 ) { if( rank == 0 ) { printf("Tamanho de bloco invalido no arquivo com a chave (%s).\n",chave_file); } goto help; } if( MPI_File_read(chave_handle,&Nk,1, MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na leitura do tamanho da chave no arquivo com a chave (%s).\n",chave_file); } goto help; } if( Nk< 4 || Nk > 8 ) { if( rank == 0 ) { printf("Tamanho de chave invalido no arquivo com a chave (%s).\n",chave_file); } goto help; } if( MPI_File_read(chave_handle,chave,4*Nk,MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na leitura da chave no arquivo com a chave (%s).\n",chave_file); } goto help; } MPI_File_close( &chave_handle ); Nr = numero_rodadas(Nb,Nk); KeyExpansion(chave,Nb,Nk); if( MPI_File_open( MPI_COMM_WORLD, entrada_file, MPI_MODE_RDONLY, MPI_INFO_NULL, &entrada_handle ) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na abertura do arquivo de entrada (%s).\n",entrada_file); } goto help; } MPI_File_get_size(entrada_handle,&entrada_bytes); if( MPI_File_open( MPI_COMM_WORLD, saida_file, MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_EXCL, MPI_INFO_NULL, &saida_handle ) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na criacao do arquivo de saida (%s).\n",saida_file); printf("Uma possivel causa eh que o arquivo ja exista.\n"); } goto help; } numero_blocos = ( entrada_bytes / (Nb*4) ); blocos_processo = numero_blocos / worldsize; if( operacao == ENCRIPTAR ) { MPI_File_set_size(saida_handle,(MPI_Offset)( (numero_blocos+1)*(Nb*4) ) ); bloco_byte_inicio = 4*Nb*blocos_processo*rank; bloco_byte_fim = 4*Nb*blocos_processo*(rank+1); for( iterador = bloco_byte_inicio ; iterador < bloco_byte_fim ; iterador += (4*Nb) ) { if( MPI_File_read_at(entrada_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao ler do arquivo de entrada (%s).\n",entrada_file); } goto help; } AES_encriptar_bloco(bloco,Nb,chave,Nr); if( MPI_File_write_at(saida_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao escrever no arquivo de saida (%s).\n",saida_file); } goto help; } } iterador = 4*Nb*blocos_processo*worldsize + 4*Nb*rank; if( iterador <= numero_blocos*4*Nb ) { if( MPI_File_read_at(entrada_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao ler do arquivo de entrada (%s).\n",entrada_file); } goto help; } if( iterador == numero_blocos*4*Nb ) bloco[ 4*Nb - 1 ] = (octeto)(entrada_bytes - numero_blocos*4*Nb); AES_encriptar_bloco(bloco,Nb,chave,Nr); if( MPI_File_write_at(saida_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao escrever no arquivo de saida (%s).\n",saida_file); } goto help; } } if( rank == 0 ) { // printf("A encriptacao do arquivo foi realizada com sucesso.\n"); } } else if( operacao == DECRIPTAR ) { MPI_File_set_size(saida_handle,entrada_bytes); bloco_byte_inicio = 4*Nb*blocos_processo*rank; bloco_byte_fim = 4*Nb*blocos_processo*(rank+1); for( iterador = bloco_byte_inicio ; iterador < bloco_byte_fim ; iterador += (4*Nb) ) { if( MPI_File_read_at(entrada_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao ler do arquivo de entrada (%s).\n",entrada_file); } goto help; } AES_decriptar_bloco(bloco,Nb,chave,Nr); if( MPI_File_write_at(saida_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao escrever no arquivo de saida (%s).\n",saida_file); } goto help; } } iterador = 4*Nb*blocos_processo*worldsize + 4*Nb*rank; if( iterador < numero_blocos*4*Nb ) { if( MPI_File_read_at(entrada_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao ler do arquivo de entrada (%s).\n",entrada_file); } goto help; } AES_decriptar_bloco(bloco,Nb,chave,Nr); if( MPI_File_write_at(saida_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao escrever no arquivo de saida (%s).\n",saida_file); } goto help; } } MPI_Barrier( MPI_COMM_WORLD ); /*Barreira q impede q alguem leia antes do valor decriptografado ser escrito */ if( MPI_File_read_at(saida_handle,entrada_bytes-1,bloco,1,MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao realizar leitura no arquivo de saida (%s).\n",saida_file); } goto help; } MPI_Barrier( MPI_COMM_WORLD ); /* Barreira q impede q alqum processo trunque o arquivo antes de outro processo ler*/ MPI_File_set_size(saida_handle,entrada_bytes - 4*Nb + bloco[0]); if( rank == 0 ) { // printf("A decriptacao do arquivo foi realizada com sucesso.\n"); } } goto finalizando; sempar: if( rank == 0 ) { printf("Sem par correspondente para a opcao %s.\n",argv[itr]); } help: if( rank == 0 ) { printf("Use a opcao --help para melhor entendimento do uso da aplicacao.\n"); } finalizando: MPI_Finalize( ); return 0; }
void qpb_write_n_spinor(qpb_spinor_field *spinor_field, int n_spinors, char fname[]) { MPI_Datatype mpi_dtype_n_spinor_float, filetype; MPI_Type_contiguous(2*NC*NS*n_spinors, MPI_FLOAT, &mpi_dtype_n_spinor_float); MPI_Type_commit(&mpi_dtype_n_spinor_float); int starts[ND], l_dim[ND], g_dim[ND]; for(int i=0; i<ND; i++) { starts[i] = problem_params.coords[i]*problem_params.l_dim[i]; l_dim[i] = problem_params.l_dim[i]; g_dim[i] = problem_params.g_dim[i]; }; int ierr = MPI_Type_create_subarray(ND, g_dim, l_dim, starts, MPI_ORDER_C, mpi_dtype_n_spinor_float, &filetype); MPI_Type_commit(&filetype); MPI_File fhandle; ierr = MPI_File_open(MPI_COMM_WORLD, fname, MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &fhandle); MPI_File_set_size(fhandle, 0); if(ierr != MPI_SUCCESS) { if(am_master) { fprintf(stderr, "%s: MPI_File_open() returned in error\n", fname); exit(QPB_FILE_ERROR); } } ierr = MPI_File_set_view(fhandle, 0, mpi_dtype_n_spinor_float, filetype, "native", MPI_INFO_NULL); if(ierr != MPI_SUCCESS) { if(am_master) { fprintf(stderr, "%s: MPI_File_set_view() returned in error\n", fname); exit(QPB_FILE_ERROR); } } void *buffer = qpb_alloc(problem_params.l_vol*n_spinors*sizeof(qpb_spinor_float)); for(int v=0; v<problem_params.l_vol; v++) for(int i=0; i<n_spinors; i++) for(int sp=0; sp<NC*NS; sp++) { ((float *) buffer)[v*n_spinors*NC*NS*2 + i*NC*NS*2 + sp*2] = spinor_field[i].bulk[v][sp].re; ((float *) buffer)[v*n_spinors*NC*NS*2 + i*NC*NS*2 + sp*2 + 1] = spinor_field[i].bulk[v][sp].im; } if(!qpb_is_bigendian()) qpb_byte_swap_float(buffer, problem_params.l_vol*n_spinors*NC*NS*2); MPI_Status status; ierr = MPI_File_write_all(fhandle, buffer, problem_params.l_vol, mpi_dtype_n_spinor_float, &status); if(ierr != MPI_SUCCESS) { if(am_master) { fprintf(stderr, "%s: MPI_File_read() returned in error\n", fname); exit(QPB_FILE_ERROR); } } ierr = MPI_File_close(&fhandle); if(ierr != MPI_SUCCESS) { if(am_master) { fprintf(stderr, "%s: MPI_File_close() returned in error\n", fname); exit(QPB_FILE_ERROR); } } free(buffer); return; }
void write_unordered_shared( const char * filename, void * local_ptr, size_t size ) { MPI_Status status; MPI_File outfile; MPI_Datatype datatype; MPI_Info info; char * local_char_ptr = static_cast< char * >( local_ptr ); // Stupid MPI uses a signed integer for the count of data elements // to write. On all the machines we use, this means the max count // is 2^31-1. To work around this, we create a datatype large // enough that we never need a count value larger than 2^30. // move this many gigabyte chunks const size_t gigabyte = 1L << 30; size_t round_count = size / gigabyte; // if there will be any bytes left over, move those too if( size & (gigabyte - 1) ) round_count++; MPI_CHECK( MPI_Info_create( &info ) ); if( FLAGS_optimize_for_lustre ) { std::map< const std::string, const std::string > info_map = {{ // disable independent file operations, since we know this // routine is the only one touching the file { "romio_no_indep_rw", "true" } // it's important to set lustre striping properly for performance. // we're supposed to be able to do this through MPI ROMIO, but mpi installations are not always configured to allow this. // if not, then before saving a file, run a command like this to create it: // lfs setstripe -c -1 -s 32m <filename> // below are what we'd like to do. // we want 32MB lustre blocks, but this probably doesn't do anything , { "striping_unit", "33554432" } // we want to use all lustre OSTs, but this probably doesn't do anything , { "striping_factor", "-1" } // // disable collective io on lustre for "small" files <= 1GB // , { "romio_lustre_ds_in_coll", "1073741825" ) ); // set collective buffering block size to something reasonable , { "cb_buffer_size", "33554432" } // // disable collective buffering for writing // , { "romio_cb_write", "disable" } // // disable collective buffering for writing // , { "romio_cb_read", "disable" } // disable data sieving for writing , { "romio_ds_write", "disable" } // disable data sieving for writing , { "romio_ds_read", "disable" } // enable direct IO , { "direct_read", "true" } , { "direct_write", "true" } // ??? // , { "romio_lustre_co_ratio", "1" } // maybe // , { "access_style", "read_once" } }}; set_mpi_info( info, info_map ); } // open file for writing int mode = MPI_MODE_CREATE | MPI_MODE_WRONLY; MPI_CHECK( MPI_File_open( global_communicator.grappa_comm, const_cast< char * >( filename ), mode, info, &outfile ) ); // drop any data previously in file MPI_CHECK( MPI_File_set_size( outfile, 0 ) ); // dump file info for debugging // if( Grappa::mycore == 0 ) { // dump_mpi_file_info( outfile ); // } // compute number of rounds required to read entire file MPI_CHECK( MPI_Allreduce( MPI_IN_PLACE, &round_count, 1, MPI_INT64_T, MPI_MAX, global_communicator.grappa_comm ) ); // write file in gigabyte-sized rounds for( int i = 0; i < round_count; ++i ) { if( size > gigabyte ) { MPI_CHECK( MPI_File_write_shared( outfile, local_char_ptr, gigabyte, MPI_BYTE, &status ) ); size -= gigabyte; local_char_ptr += gigabyte; } else { MPI_CHECK( MPI_File_write_shared( outfile, local_char_ptr, size, MPI_BYTE, &status ) ); } } MPI_CHECK( MPI_Info_free( &info ) ); MPI_CHECK( MPI_File_close( &outfile ) ); }
int main(int argc, char **argv) { if(argc < 2) { printf("Usage: %s infile\n", argv[0]); exit(1); } MPI_Comm comm = MPI_COMM_WORLD; MPI_Info mpi_info = MPI_INFO_NULL; MPI_File fh, fw; MPI_Offset file_size, frag_size, read_size; MPI_Offset offset; MPI_Status status; int retval; double start, end; unsigned char *buf, *outbuf, *outProps; size_t destlen; size_t propsize = 5; MPI_Init(&argc, &argv); MPI_Comm_rank(comm, &mpi_rank); MPI_Comm_size(comm, &mpi_size); MPI_Barrier(comm); start = MPI_Wtime(); /* * read */ MPI_File_open(comm, argv[1], MPI_MODE_RDONLY, mpi_info, &fh); MPI_File_get_size(fh, &file_size); //printf("file size:%d\n", file_size); frag_size = file_size / mpi_size; offset = frag_size * mpi_rank; read_size = MIN(frag_size, file_size - offset); //printf("rank %d offset %d\n", mpi_rank, offset); buf = malloc(frag_size + 2); assert(buf != NULL); MPI_File_open(comm, argv[1], MPI_MODE_RDONLY, mpi_info, &fh); MPI_File_read_at(fh, offset, buf, read_size, MPI_CHAR, &status); MPI_File_close(&fh); /* * compress */ destlen = 1.2 * frag_size + 1024 * 1024; outbuf = (unsigned char *)malloc(destlen); assert(outbuf != NULL); destlen = destlen - DATA_OFFSET -propsize; outProps = outbuf + DATA_OFFSET; retval = LzmaCompress(outbuf + DATA_OFFSET + propsize, &destlen, buf, read_size, outProps, &propsize, -1, 0, -1, -1, -1, -1, 1); if(retval != SZ_OK) { error_print(retval); free(buf); free(outbuf); exit(1); } /* * write */ char *fwname; unsigned long long *len; fwname = get_fwname(argv[1]); len = (unsigned long long *)outbuf; *len = read_size; //printf("%s %d\n", fwname, destlen); MPI_File_open(MPI_COMM_SELF, fwname, MPI_MODE_WRONLY | MPI_MODE_CREATE, mpi_info, &fw); MPI_File_set_size(fw, destlen); MPI_File_write(fw, outbuf, destlen + DATA_OFFSET + propsize, MPI_CHAR, &status); MPI_File_close(&fw); MPI_Barrier(comm); end = MPI_Wtime(); size_t cmprs_len; double cmprs_ratio; MPI_Reduce(&destlen, &cmprs_len, 1, MPI_UNSIGNED_LONG, MPI_SUM, 0, comm); if(0 == mpi_rank) { cmprs_ratio = (double)cmprs_len / file_size; printf("file size: %lu\n", file_size); printf("after compressed: %lu\n", cmprs_len); printf("compress ratio: %f\n", cmprs_ratio); printf("number of processes: %d\n", mpi_size); printf("time used: %fs\n", end - start); } MPI_Finalize(); free(fwname); free(buf); free(outbuf); return 0; }
int main(int argc, char *argv[]){ int i; int n_ranks; int my_rank; int data_bytes; int buffer_size; int n_iterations; char *filename_to_write=NULL; int *rank_buffer=NULL; MPI_Info my_Info=MPI_INFO_NULL; MPI_Status my_MPI_Status; MPI_File **archive_file_MPI=NULL; int file_result; long int total_data_transfer; int total_elapsed_time_s; float total_transferred_gb; float transfer_speed_gb_s; long int stage_archive_file_offset; char *archive_filenames[MAX_N_ARCHIVE_FILES]; int n_archive_files=(1); int archive_filename_length; time_t time_before,time_after; // int my_target_file; int rank_ranges[MAX_N_ARCHIVE_FILES][3]; int n_ranks_in_archive_file[MAX_N_ARCHIVE_FILES]; MPI_Group world_group; MPI_Group sub_group[MAX_N_ARCHIVE_FILES]; MPI_Comm sub_comm[MAX_N_ARCHIVE_FILES]; int my_communicator; long int total_archive_file_size; // fprintf(stderr,"parfu_write_test beginning\n"); if(argc < 6){ fprintf(stderr,"usage: \n"); fprintf(stderr," parfu_write_test <dat_bytes> <buf_bytes> <file_to_write> <n_iterations> <# arch files>\n"); MPI_Finalize(); return -1; } data_bytes=atoi(argv[1]); buffer_size=atoi(argv[2]); filename_to_write=argv[3]; n_iterations=atoi(argv[4]); n_archive_files=atoi(argv[5]); if(n_archive_files<1 || n_archive_files>MAX_N_ARCHIVE_FILES){ fprintf(stderr," you specified %d archive files! Must be >0 or <%d\n", n_archive_files,MAX_N_ARCHIVE_FILES); } MPI_Init(NULL,NULL); MPI_Comm_size(MPI_COMM_WORLD,&n_ranks); MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); if(my_rank==0){ fprintf(stderr," Data payload: %d bytes.\n",data_bytes); fprintf(stderr," Buffer size: %d bytes.\n",buffer_size); fprintf(stderr," Writing to file: >%s<\n",filename_to_write); fprintf(stderr," Performing %d iterations\n",n_iterations); } // all MPI stuff below // set up multiple file output for(i=0;i<MAX_N_ARCHIVE_FILES;i++){ archive_filenames[i]=NULL; } // Creating the sub-group communicators file_result=MPI_Comm_group(MPI_COMM_WORLD,&world_group); if(file_result != MPI_SUCCESS){ fprintf(stderr,"rank %d MPI_Comm_group to get master returned %d!\n",my_rank,file_result); } for(i=0;i<n_archive_files;i++){ rank_ranges[0][0] = i; rank_ranges[0][1] = ((n_ranks/n_archive_files)*n_archive_files) + i; if(rank_ranges[0][1] >= n_ranks){ rank_ranges[0][1] -= n_archive_files; } rank_ranges[0][2] = n_archive_files; if(my_rank == 0){ fprintf(stderr,"triple [%02d]: %4d %4d %4d\n", i,rank_ranges[0][0],rank_ranges[0][1],rank_ranges[0][2]); } n_ranks_in_archive_file[i]=((rank_ranges[0][1] - rank_ranges[0][0]) / n_archive_files)+1; file_result=MPI_Group_range_incl(world_group,1,rank_ranges,sub_group+i); if(file_result != MPI_SUCCESS){ fprintf(stderr,"rank %d MPI_Group_range_incl() returned %d\n", my_rank,file_result); } } // sub groups created; now create the sub-communicators for(i=0;i<n_archive_files;i++){ MPI_Comm_create(MPI_COMM_WORLD,sub_group[i],sub_comm+i); if(file_result != MPI_SUCCESS){ fprintf(stderr,"rank_%d MPI_Comm_create() returned %d\n", my_rank,file_result); } } my_communicator = my_rank % n_archive_files; archive_filename_length = strlen(filename_to_write) + 10; for(i=0;i<n_archive_files;i++){ if((archive_filenames[i]= (char*)malloc(sizeof(char)*archive_filename_length))==NULL){ fprintf(stderr,"Could not allocate archive_filename member # %d!\n",i); MPI_Finalize(); return -4; } sprintf(archive_filenames[i],"%s__%02d",filename_to_write,i); } // for(i=0; if(my_rank==0){ fprintf(stderr,"Writing to %d archive files:\n",n_archive_files); for(i=0;i<n_archive_files;i++){ fprintf(stderr," %s\n",archive_filenames[i]); } } // allocate transfer buffer if((rank_buffer=(void*)malloc(buffer_size))==NULL){ fprintf(stderr,"rank %d failed to allocate buffer!\n",my_rank); MPI_Finalize(); return -3; } // fill buffer with numbers for(i=0;i<(data_bytes/sizeof(int));i++){ rank_buffer[i]=(i*22)+7; } // All the ranks have a buffer ready to go // now get the collective file(s) set up for writing. if((archive_file_MPI=(MPI_File**)malloc(sizeof(MPI_File*)*n_archive_files))==NULL){ fprintf(stderr,"rank %d could not allocate array for archive file pointers!\n",my_rank); MPI_Finalize(); return 75; } for(i=0;i<n_archive_files;i++){ if((archive_file_MPI[i]=(MPI_File*)malloc(sizeof(MPI_File)))==NULL){ fprintf(stderr,"rank %d could not allocate MPI file pointer number %d!!\n",my_rank,i); return 76; } } /* for(i=0;i<n_archive_files;i++){ file_result=MPI_File_open(MPI_COMM_WORLD, archive_filenames[i], MPI_MODE_WRONLY | MPI_MODE_CREATE , my_Info, archive_file_MPI[i]); if(file_result != MPI_SUCCESS){ fprintf(stderr,"MPI_File_open for archive buffer: returned error! Rank %d file >%s<\n", my_rank, archive_filenames[i]); return 3; } } */ // all files open THEIR file in THEIR communicator total_archive_file_size = ((long int)(n_ranks_in_archive_file[my_communicator])) * ((long int)(n_iterations)) * ((long int)(buffer_size)); file_result=MPI_File_open(sub_comm[my_communicator], archive_filenames[my_communicator], MPI_MODE_WRONLY | MPI_MODE_CREATE , my_Info, archive_file_MPI[my_communicator]); if(file_result != MPI_SUCCESS){ fprintf(stderr,"MPI_File_open for archive buffer: returned error! Rank %d file >%s< comm %d\n", my_rank, archive_filenames[my_communicator],my_communicator); MPI_Finalize(); return 3; } file_result=MPI_File_set_size((*(archive_file_MPI[my_communicator])),total_archive_file_size); if(file_result != MPI_SUCCESS){ fprintf(stderr,"MPI_File_set_size for archive buffer: returned error! Rank %d file >%s< comm %d\n", my_rank, archive_filenames[my_communicator],my_communicator); MPI_Finalize(); return 4; } // file(s) is(are) open on all ranks. // time to do a whole mess of writing to it(them). MPI_Barrier(MPI_COMM_WORLD); if(my_rank==0){ fprintf(stderr,"About to begin data writing loop.\n"); time(&time_before); } /* if(n_archive_files>1){ my_target_file = my_rank % n_archive_files; } else{ my_target_file=0; } */ for(i=0;i<n_iterations;i++){ stage_archive_file_offset = ((long int)( ((long int)i) * (((long int)(n_ranks/n_archive_files)) * ((long int)buffer_size)) )) + ((long int)(( (my_rank/n_archive_files) * buffer_size))); // file_result=MPI_File_write_at_all(*archive_file_MPI,stage_archive_file_offset,rank_buffer, // data_bytes,MPI_CHAR,&my_MPI_Status); // file_result=MPI_File_write_at_all(*archive_file_MPI,stage_archive_file_offset,rank_buffer, // data_bytes,MPI_CHAR,&my_MPI_Status); file_result=MPI_File_write_at_all((*(archive_file_MPI[my_communicator])),stage_archive_file_offset,rank_buffer, data_bytes,MPI_CHAR,&my_MPI_Status); if(file_result != MPI_SUCCESS){ fprintf(stderr,"rank %d i=%d got %d from MPI_File_write_at_all\n",my_rank,i,file_result); fprintf(stderr,"failed in i=%d communicator %d!!\n",i,my_communicator); MPI_Finalize(); return 77; } if(my_rank==0 && (!(i%20))){ fprintf(stderr,"."); } } // for(i=0.... if(my_rank==0) fprintf(stderr,"\n"); // MPI_File_close((*(ar MPI_Barrier(MPI_COMM_WORLD); if(my_rank==0){ time(&time_after); total_data_transfer = ((long int)data_bytes) * ((long int)n_ranks) * ((long int)n_iterations); total_elapsed_time_s = time_after - time_before; total_transferred_gb = ((float)(total_data_transfer))/1.0e9; fprintf(stderr,"total_time: %d seconds to transfer %3.4f GB\n", total_elapsed_time_s,total_transferred_gb); transfer_speed_gb_s = ( total_transferred_gb / ((float)total_elapsed_time_s) ); fprintf(stderr,"transfer speed: %3.4f GB/s\n",transfer_speed_gb_s); } // all MPI stuff above MPI_Finalize(); return 0; }
int main(int argc, char **argv) { int buf[1024], amode, flag, mynod, len, i; MPI_File fh; MPI_Status status; MPI_Datatype newtype; MPI_Offset disp, offset; MPI_Group group; MPI_Datatype etype, filetype; char datarep[25], *filename; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &mynod); /* process 0 takes the file name as a command-line argument and broadcasts it to other processes */ if (!mynod) { i = 1; while ((i < argc) && strcmp("-fname", *argv)) { i++; argv++; } if (i >= argc) { printf("\n*# Usage: misc <mpiparameter> -- -fname filename\n\n"); MPI_Abort(MPI_COMM_WORLD, 1); } argv++; len = strlen(*argv); filename = (char *) malloc(len+1); strcpy(filename, *argv); MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD); } else { MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD); filename = (char *) malloc(len+1); MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD); } MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh); MPI_File_write(fh, buf, 1024, MPI_INT, &status); MPI_File_sync(fh); MPI_File_get_amode(fh, &amode); if (!mynod) printf("testing MPI_File_get_amode\n"); if (amode != (MPI_MODE_CREATE | MPI_MODE_RDWR)) printf("amode is %d, should be %d\n\n", amode, MPI_MODE_CREATE | MPI_MODE_RDWR); MPI_File_get_atomicity(fh, &flag); if (flag) printf("atomicity is %d, should be 0\n", flag); if (!mynod) printf("setting atomic mode\n"); MPI_File_set_atomicity(fh, 1); MPI_File_get_atomicity(fh, &flag); if (!flag) printf("atomicity is %d, should be 1\n", flag); MPI_File_set_atomicity(fh, 0); if (!mynod) printf("reverting back to nonatomic mode\n"); MPI_Type_vector(10, 10, 20, MPI_INT, &newtype); MPI_Type_commit(&newtype); MPI_File_set_view(fh, 1000, MPI_INT, newtype, "native", MPI_INFO_NULL); if (!mynod) printf("testing MPI_File_get_view\n"); MPI_File_get_view(fh, &disp, &etype, &filetype, datarep); if ((disp != 1000) || strcmp(datarep, "native")) printf("disp = %I64, datarep = %s, should be 1000, native\n\n", disp, datarep); if (!mynod) printf("testing MPI_File_get_byte_offset\n"); MPI_File_get_byte_offset(fh, 10, &disp); if (disp != (1000+20*sizeof(int))) printf("byte offset = %I64, should be %d\n\n", disp, (int) (1000+20*sizeof(int))); MPI_File_get_group(fh, &group); if (!mynod) printf("testing MPI_File_set_size\n"); MPI_File_set_size(fh, 1000+15*sizeof(int)); MPI_Barrier(MPI_COMM_WORLD); MPI_File_sync(fh); MPI_File_get_size(fh, &disp); if (disp != 1000+15*sizeof(int)) printf("file size = %I64, should be %d\n\n", disp, (int) (1000+15*sizeof(int))); if (!mynod) printf("seeking to eof and testing MPI_File_get_position\n"); MPI_File_seek(fh, 0, MPI_SEEK_END); MPI_File_get_position(fh, &disp); if (disp != 10) printf("file pointer posn = %I64, should be 10\n\n", disp); if (!mynod) printf("testing MPI_File_get_byte_offset\n"); MPI_File_get_byte_offset(fh, disp, &offset); if (offset != (1000+20*sizeof(int))) printf("byte offset = %I64, should be %d\n\n", offset, (int) (1000+20*sizeof(int))); MPI_Barrier(MPI_COMM_WORLD); if (!mynod) printf("testing MPI_File_seek with MPI_SEEK_CUR\n"); MPI_File_seek(fh, -10, MPI_SEEK_CUR); MPI_File_get_position(fh, &disp); MPI_File_get_byte_offset(fh, disp, &offset); if (offset != 1000) printf("file pointer posn in bytes = %I64, should be 1000\n\n", offset); if (!mynod) printf("preallocating disk space up to 8192 bytes\n"); MPI_File_preallocate(fh, 8192); if (!mynod) printf("closing the file and deleting it\n"); MPI_File_close(&fh); MPI_Barrier(MPI_COMM_WORLD); if (!mynod) MPI_File_delete(filename, MPI_INFO_NULL); MPI_Type_free(&newtype); MPI_Type_free(&filetype); MPI_Group_free(&group); free(filename); MPI_Finalize(); return 0; }
int main(int argc,char* argv[]) { char* params = NULL; char gauge_name[qcd_MAX_STRING_LENGTH]; char param_name[qcd_MAX_STRING_LENGTH]; char out_name[qcd_MAX_STRING_LENGTH]; qcd_int_4 x_src[4],lx_src[4],i,nsmear,nsmearAPE; qcd_real_8 alpha ,alphaAPE,plaq; int params_len; qcd_geometry geo; qcd_gaugeField u, uAPE; qcd_gaugeField *u_ptr, *uAPE_ptr, *utmp_ptr; qcd_vector vec; qcd_uint_2 P[4]; qcd_uint_2 L[4]; qcd_real_8 theta[4]={M_PI,0.,0.,0.}; // boundary conditions int myid,numprocs, namelen; char processor_name[MPI_MAX_PROCESSOR_NAME]; //set up MPI MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); // num. of processes taking part in the calculation MPI_Comm_rank(MPI_COMM_WORLD,&myid); // each process gets its ID MPI_Get_processor_name(processor_name,&namelen); // //////////////////// READ INPUT FILE ///////////////////////////////////////////// if(argc!=2) { if(myid==0) fprintf(stderr,"No input file specified\n"); exit(EXIT_FAILURE); } strcpy(param_name,argv[1]); if(myid==0) { i=0; printf("opening input file %s\n",param_name); params=qcd_getParams(param_name,¶ms_len); if(params==NULL) { i=1; } } MPI_Bcast(&i,1,MPI_INT, 0, MPI_COMM_WORLD); if(i==1) exit(EXIT_FAILURE); MPI_Bcast(¶ms_len, 1, MPI_INT, 0, MPI_COMM_WORLD); if(myid!=0) params = (char*) malloc(params_len*sizeof(char)); MPI_Bcast(params, params_len, MPI_CHAR, 0, MPI_COMM_WORLD); sscanf(qcd_getParam("<processors_txyz>",params,params_len),"%hd %hd %hd %hd",&P[0], &P[1], &P[2], &P[3]); if(P[0] != 1) { fprintf(stderr, " Must use only 1 process along t-direction, exiting...\n"); exit(EXIT_FAILURE); } sscanf(qcd_getParam("<lattice_txyz>",params,params_len),"%hd %hd %hd %hd",&L[0], &L[1], &L[2], &L[3]); if(qcd_initGeometry(&geo,L,P, theta, myid, numprocs)) exit(EXIT_FAILURE); if(myid==0) printf(" Local lattice: %i x %i x %i x %i\n",geo.lL[0],geo.lL[1],geo.lL[2],geo.lL[3]); sscanf(qcd_getParam("<source_pos_txyz>",params,params_len),"%d %d %d %d",&x_src[0], &x_src[1], &x_src[2], &x_src[3]); if(myid==0) printf(" Got source coords: %d %d %d %d\n",x_src[0],x_src[1],x_src[2],x_src[3]); sscanf(qcd_getParam("<alpha_gauss>",params,params_len),"%lf", &alpha); if(myid==0) printf(" Got alpha_gauss: %lf\n",alpha); sscanf(qcd_getParam("<nsmear_gauss>",params,params_len),"%d",&nsmear); if(myid==0) printf(" Got nsmear_gauss: %d\n",nsmear); sscanf(qcd_getParam("<alpha_APE>",params,params_len),"%lf",&alphaAPE); if(myid==0) printf(" Got alpha_APE: %lf\n",alphaAPE); sscanf(qcd_getParam("<nsmear_APE>",params,params_len),"%d",&nsmearAPE); if(myid==0) printf(" Got nsmear_APE: %d\n",nsmearAPE); strcpy(gauge_name,qcd_getParam("<cfg_name>",params,params_len)); if(myid==0) printf(" Got conf name: %s\n",gauge_name); strcpy(out_name,qcd_getParam("<src_block_name>",params,params_len)); if(myid==0) printf(" Got out name: %s\n",out_name); free(params); /////////////////////////////////////////////////////////////////////////////////////////////////// if(nsmear != 0) { qcd_initGaugeField(&u,&geo); qcd_initGaugeField(&uAPE,&geo); if(qcd_getGaugeField(gauge_name,qcd_GF_LIME,&u)) { fprintf(stderr,"process %i: Error reading gauge field!\n",myid); exit(EXIT_FAILURE); } if(myid==0) printf("gauge-field loaded\n"); plaq = qcd_calculatePlaquette(&u); if(myid==0) printf("plaquette = %e\n",plaq); u_ptr = &u; uAPE_ptr = &uAPE; for(i=0; i<nsmearAPE; i++) { qcd_apeSmear3d(uAPE_ptr, u_ptr, alphaAPE); utmp_ptr=u_ptr; u_ptr=uAPE_ptr; uAPE_ptr=utmp_ptr; } utmp_ptr=u_ptr; u_ptr=uAPE_ptr; uAPE_ptr=utmp_ptr; //reverse the last swap. Also needed when nsmearAPE=0 qcd_destroyGaugeField(u_ptr); uAPE = *uAPE_ptr; if(myid==0) printf("gauge-field APE-smeared\n"); plaq = qcd_calculatePlaquette(&uAPE); if(myid==0) printf("plaquette = %e\n",plaq); //qcd_initPropagator(&source,&geo); } qcd_initVector(&vec,&geo); // which process has the source coords? for(i=0; i<4; i++) lx_src[i] = x_src[i]/geo.lL[i]; qcd_zeroVector(&vec); if( (lx_src[0]==geo.Pos[0]) && (lx_src[1]==geo.Pos[1]) && (lx_src[2]==geo.Pos[2]) && (lx_src[3]==geo.Pos[3]) ) { vec.D[qcd_LEXIC((x_src[0]%geo.lL[0]), (x_src[1]%geo.lL[1]), (x_src[2]%geo.lL[2]), (x_src[3]%geo.lL[3]), geo.lL)][0][0].re=1.; } for(i=0; i<nsmear; i++) { if(qcd_gaussIteration3d(&vec,&uAPE,alpha,x_src[0])) { fprintf(stderr,"process %i: Error while smearing!\n",geo.myid); exit(EXIT_FAILURE); } } qcd_real_8 *src = malloc(sizeof(qcd_real_8)*geo.lL[1]*geo.lL[2]*geo.lL[3]); if(src == NULL) { fprintf(stderr, "process %i: malloc returned NULL!\n",geo.myid); exit(EXIT_FAILURE); } if( lx_src[0]==geo.Pos[0] ) { for(int z=0; z<geo.lL[3]; z++) for(int y=0; y<geo.lL[2]; y++) for(int x=0; x<geo.lL[1]; x++) { int lv = qcd_LEXIC((x_src[0]%geo.lL[0]), x, y, z, geo.lL); double loc_norm = 0; for(int mu=0; mu<4; mu++) for(int c=0; c<3; c++) { loc_norm += qcd_NORMSQUARED(vec.D[lv][mu][c]); } src[x+geo.lL[1]*(y+z*geo.lL[2])] = loc_norm; } } qcd_destroyVector(&vec); /* * This assumes only 1 process in time * If not, anything may happen */ MPI_Datatype fileview; MPI_File fh; MPI_Status status; int globv3[] = {geo.L[3], geo.L[2], geo.L[1]}; int locv3[] = {geo.lL[3], geo.lL[2], geo.lL[1]}; int starts[] = {geo.Pos[3]*locv3[0], geo.Pos[2]*locv3[1], geo.Pos[1]*locv3[2]}; MPI_Type_create_subarray(3, globv3, locv3, starts, MPI_ORDER_C, MPI_DOUBLE, &fileview); MPI_Type_commit(&fileview); MPI_File_open(MPI_COMM_WORLD, out_name, MPI_MODE_WRONLY|MPI_MODE_CREATE, MPI_INFO_NULL, &fh); MPI_File_set_size(fh, 0); MPI_File_set_view(fh, 0, MPI_DOUBLE, fileview, "native", MPI_INFO_NULL); if(!qcd_isBigEndian()) { qcd_swap_8(src, (geo.lL[1]*geo.lL[2]*geo.lL[3])); } MPI_File_write_all(fh, src, (geo.lL[1]*geo.lL[2]*geo.lL[3]), MPI_DOUBLE, &status); MPI_File_close(&fh); free(src); if(nsmear != 0) qcd_destroyGaugeField(&uAPE); ////////////////////////////////////// CLEAN UP AND EXIT /////////////////////////////////////////// //qcd_destroyPropagator(&source); qcd_destroyGeometry(&geo); MPI_Finalize(); return(EXIT_SUCCESS); }//end main
int main(int argc, char *argv[]) { int nrank; /* Общее количество процессов */ int myrank; /* Номер текущего процесса */ unsigned char* channel[2][3]; int i, j, k; char buffer[BUFFER_SIZE]; MPI_Status status; MPI_Request req; MPI_File inFile; MPI_File outFile; MPI_Offset pos; /* Иницилизация MPI */ MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nrank); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); if (myrank == 0 && argc < 6) { printf("Число процессов :\t%d\n", nrank); printf("Инструкция по применению :\t%s filter step width height inputfilename.bin ouputfilename.bin\n", argv[0]); } if (argc < 6) { MPI_Finalize(); exit(-1); } char *filter = argv[1]; int step = atoi(argv[2]); int width = atoi(argv[3]); int height = atoi(argv[4]); char *inputFileName = argv[5]; char *outputFileName = argv[6]; if (myrank == 0) { printf("Название :\t%s\n", title); printf("Описание :\t%s\n", description); printf("Фильтр :\t%s\n", filter); printf("Шаг :\t%d\n", step); printf("Размеры :\t%d %d\n", width, height); printf("Имя входного файла :\t%s\n", inputFileName); printf("Имя выходного файла :\t%s\n", outputFileName); } for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) channel[i][j] = (unsigned char*)malloc(width*height*sizeof(char)); int N = step % 2 == 0 ? step += 1 : step; int Nh = N / 2; int x1, x2, x3, y1, y2, y3, n = N * N / 2; unsigned char* rgb[3]; for (j = 0; j < 3; j++) rgb[j] = (unsigned char*)malloc(N * N*sizeof(char)); long total = (width - 2 * Nh)*(height - 2 * Nh); long startId = myrank*total / nrank; long endId = (myrank + 1)*total / nrank; int Y1 = (startId / (width - 2 * Nh)); int X1 = 0; int Y2 = ((endId + width - 2 * Nh - 1) / (width - 2 * Nh)) + 2 * Nh; int X2 = 0; MPI_Offset offset = Y1*width + X1; MPI_Offset count = Y2*width + X2 - offset; int rc = MPI_File_open(MPI_COMM_WORLD, inputFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inFile); if (rc) { fprintf(stderr, "Ошибка открытия файла (%s)\n", inputFileName); fflush(stderr); MPI_Finalize(); exit(-1); } MPI_File_seek(inFile, 3*offset, MPI_SEEK_SET); for (pos = offset; pos < offset + count; pos += BUFFER_SIZE/3){ long size = min(BUFFER_SIZE / 3, offset + count - (int)pos); MPI_File_read(inFile, buffer, 3 * size, MPI_BYTE, &status); for (j = 0; j < 3; j++) for (k = 0; k < size; k++) channel[0][j][pos + k] = buffer[3 * k + j]; } MPI_File_close(&inFile); int id; for (id = startId; id<endId; id++) { y1 = (id / (width - 2 * Nh)) + Nh; x1 = (id % (width - 2 * Nh)) + Nh; i = 0; for (y2 = -Nh; y2 <= Nh; y2++) { y3 = y1 + y2; for (x2 = -Nh; x2 <= Nh; x2++) { x3 = x1 + x2; for (j = 0; j < 3; j++) rgb[j][i] = channel[0][j][y3*width + x3]; i++; } } for (j = 0; j < 3; j++) qsort(rgb[j], N*N, sizeof(char), compareBytes); for (j = 0; j < 3; j++) channel[1][j][y1*width + x1] = rgb[j][n]; } Y1 = (startId / (width - 2 * Nh)) + Nh; X1 = (startId % (width - 2 * Nh)) + Nh; Y2 = (endId / (width - 2 * Nh)) + Nh; X2 = (endId % (width - 2 * Nh)) + Nh; offset = Y1*width + X1; count = Y2*width + X2 - offset; rc = MPI_File_open(MPI_COMM_WORLD, outputFileName, MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &outFile); if (rc) { fprintf(stderr, "Ошибка открытия файла (%s)\n", outputFileName); fflush(stderr); MPI_Finalize(); exit(-1); } MPI_File_set_size(outFile, 3 * width*height); MPI_File_seek(outFile, 3*offset, MPI_SEEK_SET); for (pos = offset; pos < offset + count; pos += BUFFER_SIZE/3){ long size = min(BUFFER_SIZE / 3, offset + count - pos); for (j = 0; j < 3; j++) for (k = 0; k < size; k++) buffer[3 * k + j] = channel[1][j][pos + k]; MPI_File_write(outFile, buffer, 3 * size, MPI_BYTE, &status); } MPI_File_close(&outFile); MPI_Finalize(); exit(0); }
int main(int argc, char **argv) { MPI_File fp; LemonWriter *w; LemonReader *r; LemonRecordHeader *h; double *data; double tick, tock; double *timesRead; double *timesWrite; double stdRead = 0.0; double stdWrite = 0.0; int mpisize; int rank; char const *type; int ldsize; unsigned long long int fsize; int *hashMatch, *hashMatchAll; double const rscale = 1.0 / RAND_MAX; int ME_flag=1, MB_flag=1, status=0; int latDist[] = {0, 0, 0, 0}; int periods[] = {1, 1, 1, 1}; int locSizes[4]; int latSizes[4]; int localVol = 1; int latVol = localVol; MPI_Comm cartesian; int i, j; md5_state_t state; md5_byte_t before[16]; md5_byte_t after[16]; int L; int iters; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &mpisize); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (argc != 3) { usage(rank, argv); MPI_Finalize(); return 1; } L = atoi(argv[1]); if (L <= 0) usage(rank, argv); iters = atoi(argv[2]); if (iters <= 0) usage(rank, argv); timesWrite = (double*)calloc(iters, sizeof(double)); if (timesWrite == (double*)NULL) { fprintf(stderr, "ERROR: Could not allocate memory.\n"); return 1; } timesRead = (double*)calloc(iters, sizeof(double)); if (timesRead == (double*)NULL) { fprintf(stderr, "ERROR: Could not allocate memory.\n"); return 1; } hashMatch = (int*)calloc(iters, sizeof(int)); if (hashMatch == (int*)NULL) { fprintf(stderr, "ERROR: Could not allocate memory.\n"); return 1; } hashMatchAll = (int*)calloc(iters, sizeof(int)); if (hashMatchAll == (int*)NULL) { fprintf(stderr, "ERROR: Could not allocate memory.\n"); return 1; } /* Construct a Cartesian topology, adjust lattice sizes where needed */ MPI_Dims_create(mpisize, 4, latDist); for (i = 0; i < 4; ++i) { int div = (i == 3 ? (2 * L) : L) / latDist[i]; locSizes[i] = div ? div : 1; localVol *= locSizes[i]; latSizes[i] = locSizes[i] * latDist[i]; } latVol = mpisize * localVol; ldsize = localVol * 72 * sizeof(double); fsize = (unsigned long long int)latVol * 72 * sizeof(double); MPI_Cart_create(MPI_COMM_WORLD, 4, latDist, periods, 1, &cartesian); MPI_Comm_rank(cartesian, &rank); if (rank == 0) { fprintf(stdout, "Benchmark on a block of data %s in size,\n", humanForm(fsize)); fprintf(stdout, "representing a %u x %u x %u x %u lattice", latSizes[0], latSizes[1], latSizes[2], latSizes[3]); if (mpisize == 1) fprintf(stdout, ".\n\n"); else { fprintf(stdout, ",\ndistributed over %u MPI processes\n", mpisize); fprintf(stdout, "for a local %u x %u x %u x %u lattice.\n\n", locSizes[0], locSizes[1], locSizes[2], locSizes[3]); } } /* Allocate a block of memory for dummy data to write */ data = (double*)malloc(ldsize); if (data == (double*)NULL) { fprintf(stderr, "ERROR: Could not allocate memory.\n"); return 1; } srand(time(NULL) + rank); /* Start of test */ for (i = 0; i < iters; ++i) { if (rank == 0) fprintf(stdout, "Measurement %d of %d.\n", i + 1, iters); /* Create a block of dummy data to write out Fill with some random numbers to make sure we don't get coincidental matches here */ for (j = 0; j < (localVol * 72); ++j) data[j] = rscale * (double)rand(); /* Calculate a hash of the data, to check integrity against */ md5_init(&state); md5_append(&state, (md5_byte_t const *)data, ldsize); md5_finish(&state, before); /* Note that the following is the only (?) way to truncate the file with MPI */ MPI_File_open(cartesian, "benchmark.test", MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &fp); MPI_File_set_size(fp, 0); w = lemonCreateWriter(&fp, cartesian); h = lemonCreateHeader(MB_flag, ME_flag, "benchmark", latVol); status = lemonWriteRecordHeader(h, w); lemonDestroyHeader(h); MPI_Barrier(cartesian); tick = MPI_Wtime(); lemonWriteLatticeParallel(w, data, 72 * sizeof(double), latSizes); tock = MPI_Wtime(); MPI_Barrier(cartesian); timesWrite[i] = tock - tick; if (rank == 0) fprintf(stdout, "Time spent writing was %4.2g s.\n", timesWrite[i]); lemonWriterCloseRecord(w); lemonDestroyWriter(w); MPI_File_close(&fp); /* Clear data to avoid an utterly failed read giving md5 hash matches from the old data */ memset(data, 0, ldsize); /* Start of reading test */ MPI_File_open(cartesian, "benchmark.test", MPI_MODE_RDONLY | MPI_MODE_DELETE_ON_CLOSE, MPI_INFO_NULL, &fp); r = lemonCreateReader(&fp, cartesian); if (lemonReaderNextRecord(r)) fprintf(stderr, "Node %d reports: next record failed.\n", rank); type = lemonReaderType(r); if (strncmp(type, "benchmark", 13)) fprintf(stderr, "Node %d reports: wrong type read.\n", rank); MPI_Barrier(cartesian); tick = MPI_Wtime(); lemonReadLatticeParallel(r, data, 72 * sizeof(double), latSizes); tock = MPI_Wtime(); timesRead[i] = tock - tick; MPI_Barrier(cartesian); if (rank == 0) fprintf(stdout, "Time spent reading was %4.2g s.\n", timesRead[i]); lemonDestroyReader(r); MPI_File_close(&fp); md5_init(&state); md5_append(&state, (md5_byte_t const *)data, ldsize); md5_finish(&state, after); hashMatch[i] = strncmp((char const *)before, (char const *)after, 16) != 0 ? 1 : 0; MPI_Reduce(hashMatch + i, hashMatchAll + i, 1, MPI_INT, MPI_SUM, 0, cartesian); if (rank == 0) { if (hashMatchAll[i] == 0) fprintf(stdout, "All nodes report that MD5 hash matches.\n\n"); else fprintf(stdout, "WARNING: MD5 hash failure detected!\n\n"); } } /* Aggregate the data */ hashMatch[0] = 0; stdWrite = timesWrite[0] * timesWrite[0]; stdRead = timesRead[0] * timesRead[0]; for (i = 1; i < iters; ++i) { hashMatchAll[0] += hashMatchAll[i]; timesWrite[0] += timesWrite[i]; stdWrite += timesWrite[i] * timesWrite[i]; timesRead[0] += timesRead[i]; stdRead += timesRead[i] * timesRead[i]; } stdWrite /= iters; stdRead /= iters; timesWrite[0] /= iters; timesRead[0] /= iters; stdWrite -= timesWrite[0] * timesWrite[0]; stdRead -= timesRead[0] * timesRead[0]; if (rank == 0) { fprintf(stdout, "Average time spent writing was %4.2e s, ", timesWrite[0]); fprintf(stdout, "with a standard deviation of %4.2e s.\n", sqrt(stdWrite)); fprintf(stdout, "Average time spent reading was %4.2e s, ", timesRead[0]); fprintf(stdout, "with a standard deviation of %4.2e s.\n\n", sqrt(stdRead)); stdWrite *= (double)fsize / (timesWrite[0] * timesWrite[0]); stdRead *= (double)fsize / (timesRead[0] * timesRead[0]); fprintf(stdout, "Average writing speed was %s/s\n", humanForm((unsigned long long int)(fsize / timesWrite[0]))); fprintf(stdout, "Average reading speed was %s/s\n", humanForm((unsigned long long int)(fsize / timesRead[0]))); if (hashMatchAll[0] == 0) fprintf(stdout, "All data hashed correctly.\n"); else fprintf(stdout, "WARNING: %d hash mismatches detected!.\n", hashMatchAll[0]); } MPI_Finalize(); free(data); free(timesWrite); free(timesRead); free(hashMatch); free(hashMatchAll); return(0); }
int main(int argc, char* argv[]) { int i, rank, npes, bug=0; int buf[ng]; MPI_File thefile; MPI_Status status; MPI_Datatype filetype; MPI_Comm new_comm; MPI_Offset offset=0; MPI_Info info=MPI_INFO_NULL; int gsize[D],distrib[D],dargs[D],psize[D]; int dims[D],periods[D],reorder; double t1,t2,mbs; double to1,to2,tc1,tc2; double et,eto,etc; double max_mbs,min_mbs,avg_mbs; double max_et,min_et,avg_et; double max_eto,min_eto,avg_eto; double max_etc,min_etc,avg_etc; char process_name[MPI_MAX_PROCESSOR_NAME + 1]; char rr_blank[] = {" "}; char rr_empty[] = {"???????"}; int count; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &npes); if ( rank == 0 ) { if ( argc < 2 ) { printf(" ERROR: no filename given\n"); bug++; } if ( npes == np ) { printf(" file name: %s\n",argv[1]); printf(" total number of PE's: %3d\n",np); printf(" number of PE's in x direction: %4d\n",npx); printf(" number of PE's in y direction: %4d\n",npy); printf(" number of PE's in z direction: %4d\n",npz); printf(" global grid size: %dx%dx%d 4 byte integers (total %lld)\n",X,Y,Z,(unsigned long)X*Y*Z); printf(" local grid size: %dx%dx%d 4 byte integers (total %d)\n",nx,ny,nz,ng); } else { printf(" ERROR: total number of PE's must be %d\n",np); printf(" actual number of PE's was %d\n",npes); bug++; } if ( bug ) { MPI_Abort(MPI_COMM_WORLD,-1); } } if ( MPI_Get_processor_name(process_name, &count) != MPI_SUCCESS) { sprintf(process_name, rr_empty); } else { if (count < MAX_RR_NAME) strncat(&process_name[count],rr_blank,MAX_RR_NAME-count); process_name[MAX_RR_NAME] = '\0'; } MPI_Barrier(MPI_COMM_WORLD); MPI_Info_create(&info); /* allow multiple writers to write to the file concurrently */ /*MPI_Info_set(info,"panfs_concurrent_write","1");*/ /* use data aggregation */ /*MPI_Info_set(info,"romio_cb_write","enable"); */ /*MPI_Info_set(info,"romio_cb_write","disable");*/ /*MPI_Info_set(info,"romio_cb_read","enable"); */ /*MPI_Info_set(info,"romio_cb_read","disable");*/ /* use one aggregator/writer per node */ /*MPI_Info_set(info,"cb_config_list","*:1");*/ /* aggregators/writers per allocation: use this or the above (both work) */ /*i = ((npes-1)/8) + 1; sprintf(awpa,"%d",i); MPI_Info_set (info,"cb_nodes",awpa);*/ for ( i=0; i<ng; i++ ) buf[i] = rank*10000 + (i+1)%1024; for ( i=0; i<D; i++ ) { periods[i] = 1; /* true */ } reorder = 1; /* true */ dims[0] = npx; dims[1] = npy; dims[2] = npz; MPI_Cart_create(MPI_COMM_WORLD, D, dims, periods, reorder, &new_comm); for ( i=0; i<D; i++ ) { distrib[i] = MPI_DISTRIBUTE_BLOCK; dargs[i] = MPI_DISTRIBUTE_DFLT_DARG; /* psize[i] = 0; */ } gsize[0] = X; gsize[1] = Y; gsize[2] = Z; psize[0] = npx; psize[1] = npy; psize[2] = npz; /* MPI_Dims_create(npes, D, psize); printf("psize %d %d %d\n",psize[0],psize[1],psize[2]); */ MPI_Type_create_darray(npes, rank, D, gsize, distrib, dargs, psize, MPI_ORDER_FORTRAN, MPI_INT, &filetype); /*MPI_Type_create_darray(npes, rank, D, gsize, distrib, dargs, psize, MPI_ORDER_C, MPI_INT, &filetype); don't do this */ MPI_Type_commit(&filetype); to1 = MPI_Wtime(); MPI_File_open(new_comm, argv[1], MPI_MODE_WRONLY | MPI_MODE_CREATE, info, &thefile); to2 = MPI_Wtime(); MPI_File_set_size(thefile, offset); MPI_File_set_view(thefile, offset, MPI_INT, filetype, "native", MPI_INFO_NULL); t1 = MPI_Wtime(); for ( i=0; i<LOOP; i++) { MPI_File_write_all(thefile, buf, ng, MPI_INT, &status); } t2 = MPI_Wtime(); tc1 = MPI_Wtime(); MPI_File_close(&thefile); tc2 = MPI_Wtime(); et = (t2 - t1)/LOOP; eto = (to2 - to1)/LOOP; etc = (tc2 - tc1)/LOOP; mbs = (((double)(LOOP*X*Y*Z)*sizeof(int)))/(1000000.0*(t2-t1)); /*printf(" %s[%3d] ET %8.2f %8.2f %8.2f %8.1f mbs\n", process_name, rank, t1, t2, t2-t1, mbs);*/ MPI_Barrier(MPI_COMM_WORLD); MPI_Reduce(&mbs, &avg_mbs, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); MPI_Reduce(&mbs, &min_mbs, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD); MPI_Reduce(&mbs, &max_mbs, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD); MPI_Reduce(&et, &avg_et, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); MPI_Reduce(&et, &min_et, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD); MPI_Reduce(&et, &max_et, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD); MPI_Reduce(&eto, &avg_eto, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); MPI_Reduce(&eto, &min_eto, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD); MPI_Reduce(&eto, &max_eto, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD); MPI_Reduce(&etc, &avg_etc, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); MPI_Reduce(&etc, &min_etc, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD); MPI_Reduce(&etc, &max_etc, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD); fflush(stdout); if ( rank == 0 ) { mbs = avg_mbs/npes; printf("\n average write rate: %9.1f mbs\n", mbs); printf(" minimum write rate: %9.1f mbs\n", min_mbs); printf(" maximum write rate: %9.1f mbs\n\n", max_mbs); avg_eto = avg_eto/npes; avg_et = avg_et/npes; avg_etc = avg_etc/npes; printf(" open time: %9.3f min %9.3f avg %9.3f max\n",min_eto,avg_eto,max_eto); printf(" write time: %9.3f min %9.3f avg %9.3f max\n",min_et,avg_et,max_et); printf(" close time: %9.3f min %9.3f avg %9.3f max\n\n",min_etc,avg_etc,max_etc); fflush(stdout); } MPI_Finalize(); return 0; }
int main(int argc, char** argv){ int irank, nrank; MPI_Init (&argc, &argv); MPI_Comm_size (MCW, &nrank); MPI_Comm_rank (MCW, &irank); double t1,t2; if(irank==0) t1 = MPI_Wtime(); int nx, ny; int px, py; /* コピペゾーン */ // (1) init dims int dims[2] = {0,0}; MPI_Dims_create(nrank,2,dims); ny = (NY-1)/dims[0]; nx = (NX-1)/dims[1]; // (2) init cart int periods[2] = {0,0}; // 非周期境界 MPI_Comm cart; MPI_Cart_create(MCW, 2, dims, periods, 0, &cart); int c[2]; /* 座標 */ MPI_Cart_coords(cart, irank, 2, c); py = c[0]; // c[2]は大きい順なのでyがc[0] px = c[1]; double h = 1.0/NX; double dt = 0.1*h*h; double dth2 = dt/h/h; int i,j,k; int height = ny+2, width = nx+2; double (*u)[width]; u = (double(*)[width])malloc(height*width*sizeof(double)); u = (double(*)[width])(&u[1][1]); double (*un)[width]; un = (double(*)[width])malloc(height*width*sizeof(double)); un = (double(*)[width])(&un[1][1]); for (j=-1;j<ny+1;j++) for (i=-1;i<nx+1;i++){ u[j][i] = 0.0; } // (y=0) if (py==0) for (i=-1;i<nx+1;i++){ u[-1][i] = 1.0; } // (x=0) if (px==0) for (j=0;j<ny+1;j++){ u[j][-1] = 0.5; } MPI_Datatype vedge; MPI_Type_vector(ny, 1, nx+2, MPI_DOUBLE, &vedge); MPI_Type_commit(&vedge); int north, south, east, west; MPI_Cart_shift(cart,0,1,&south,&north); MPI_Cart_shift(cart,1,1,&west,&east); /* loop start */ for (k=0; k<2000; k++){ for (j=0; j<ny; j++){ for (i=0; i<nx; i++) un[j][i] = u[j][i] + ( -4*u[j][i] + u[j][i+1] + u[j][i-1] + u[j+1][i] + u[j-1][i] )*dth2; } for (j=0; j<ny; j++){ for (i=0; i<nx; i++) u[j][i] = un[j][i]; } MPI_Sendrecv(&u[ny-1][0], nx, MPI_DOUBLE, north, 0, &u[-1][0], nx, MPI_DOUBLE, south, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Sendrecv(&u[0][0], nx, MPI_DOUBLE, south, 0, &u[ny][0], nx, MPI_DOUBLE, north, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Sendrecv(&u[0][nx-1], 1, vedge, east, 0, &u[0][-1], 1, vedge, west, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Sendrecv(&u[0][0], 1, vedge, west, 0, &u[0][nx], 1, vedge, east, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); } // end loop(k) /* setview */ MPI_File udata; MPI_File_open(cart, "u.data", MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &udata); MPI_File_set_size(udata,0); int size[2] = {NY+1, LW*(NX+1)+1}, subsize[2], start[2]; // ... py,pxはcartesian座標 subsize[0] = ny; subsize[1] = LW*nx; start[0] = py*ny+1; start[1] = LW*(px*nx+1); if (py == 0){ subsize[0]++; start[0]=0; } /* 南端↓ */ if (py == dims[0]-1) subsize[0]++; /* 北端↑ */ if (px == 0){ subsize[1]+=LW; start[1]=0; } /* 西端← */ if (px == dims[1]-1) subsize[1]+=LW+1; /* 東端→ */ MPI_Datatype ftype; MPI_Type_create_subarray(2, size, subsize, start, MPI_ORDER_C, MPI_CHAR, &ftype); MPI_Type_commit(&ftype); MPI_File_set_view(udata, 0, MPI_CHAR, ftype, "native", MPI_INFO_NULL); /* output */ MPI_Status st; char *wbuf = (char*)malloc((LW*(nx+2)+2)*sizeof(char)); int jstart=0,istart=0, jend=ny, iend=nx; if(py==0) jstart = -1; if(py==dims[0]-1) jend = ny+1; if(px==0) istart = -1; if(px==dims[1]-1) iend = nx+1; for(j=jstart; j<jend; j++){ for(i=istart,k=0; i<iend; i++,k+=LW){ sprintf( wbuf+k, " %.15E %.15E %21.15E\n", (i+1 + px*nx)*h, (j+1 + py*ny)*h, u[j][i] ); } if( px == dims[1]-1 ) // 東端→ sprintf(wbuf+(k++),"\n"); MPI_File_write(udata,wbuf,k,MPI_CHAR,&st); } MPI_File_close(&udata); if(irank==0){ t2 = MPI_Wtime(); printf("%g\n",t2-t1); } MPI_Finalize (); return 0; }
int main( int argc, char *argv[] ) { unsigned int itr; int operacao; int verbose; int juntar; char * chave_file; char * entrada_file; char * saida_file; octeto Nb,Nk,Nr; octeto bloco[4*8]; octeto chave[4*8*15]; int worldsize, rank; MPI_Status status; MPI_File chave_handle; MPI_File entrada_handle; MPI_File saida_handle; MPI_Offset entrada_bytes; unsigned int numero_blocos; unsigned int blocos_processo; MPI_Offset bloco_byte_inicio; MPI_Offset bloco_byte_fim; MPI_Offset iterador; Tabela * tabela; octeto * tabelaEmpacotada; unsigned int proc; unsigned int tamanho_tabela; Tabela * tabela2; unsigned int no_proc; unsigned int no_resto; unsigned int i; BTreeNode * node; Indice * indice; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&worldsize); MPI_Comm_rank(MPI_COMM_WORLD,&rank); operacao = INDEFINIDA; verbose = 0; juntar = 0; chave_file = NULL; entrada_file = NULL; saida_file = NULL; for(itr = 1;itr < (unsigned int)argc;itr++) { /* Instrucoes de uso */ if( strcmp(argv[itr],"-a") == 0 || strcmp(argv[itr],"--ajuda") == 0 || strcmp(argv[itr],"-h") == 0 || strcmp(argv[itr],"--help") == 0 ) { if(rank == 0) { printf(" Uso: mpiexec -n [PROCESSOS] ./sm-rijndael [ARGUMENTO VALOR].\n"); printf(" Encripta/Decripta um arquivo usando o algoritmo Rijndael(AES) extendido,\n"); printf(" realizando um pre-processamento de blocos repetidos.\n"); printf(" Argumentos opcionais:\n"); printf(" -v,--verbose: Exibe mensagens de conclusao da operacao.\n"); printf(" -j,--juntar: Concatena as tabelas de cada processo em um mestre.\n"); printf(" Argumentos obrigatorios:\n"); printf(" -op,--operacao: Informa se o objetivo da execucao eh encriptar ou decriptar.\n"); printf(" * Os valores possiveis sao: \'encriptar\' e \'decriptar\'.\n"); printf(" -e,-i,--entrada,--input: Caminho e nome do arquivo a ser criptografado.\n"); printf(" -s,-o,--saida,--output: Caminho e nome do arquivo resultante do processo de criptografia da entrada.\n"); printf(" -c,-k,--chave,--key: Caminho e nome do arquivo contendo a chave.\n"); printf(" O arquivo contendo a chave eh em formato binario de acordo com a seguinte especificacao:\n"); printf(" - O primeiro byte deve conter o tamanho do bloco (em palavras de 4 bytes).\n"); printf(" * O bloco pode possuir tamanho: 4, 5, 6, 7 ou 8.\n"); printf(" - O segundo byte deve conter o tamanho da chave (em palavras de 4 bytes).\n"); printf(" * Esta aplicacao aceita chaves com tamanho: 4, 5, 6, 7 ou 8.\n"); printf(" - Os proximos 4*[tamanho da chave] bytes do arquivo sao os bytes componentes da chave, que\n"); printf(" devem estar (obrigatoriamente) escritos no formato hexadecimal da linguagem C (0xff).\n"); printf(" * Eh recomendavel o uso de um editor hexadecimal na construcao do arquivo chave.\n"); } goto finalizando; } /* Juntar: Concatena as tabelas de cada processo em um mestre */ else if( strcmp(argv[itr],"-j") == 0 || strcmp(argv[itr],"--juntar") == 0) { juntar = 1; } /* Verbose: exibir mensagens de finalizacao */ else if( strcmp(argv[itr],"-v") == 0 || strcmp(argv[itr],"--verbose") == 0) { verbose = 1; } /* Operacao a ser realizada */ else if( strcmp(argv[itr],"-op") == 0 || strcmp(argv[itr],"--operacao") == 0 ) { if( itr+1 < argc ) { if( strcmp(argv[itr+1],"encriptar") == 0 ) { operacao = ENCRIPTAR; } else if( strcmp(argv[itr+1],"decriptar") == 0 ) { operacao = DECRIPTAR; } itr++; } else { goto sempar; } } /* Arquivo com a chave */ else if( strcmp(argv[itr],"-c") == 0 || strcmp(argv[itr],"--chave") == 0 || strcmp(argv[itr],"-k") == 0 || strcmp(argv[itr],"--key") == 0 ) { if(itr+1 < argc) { chave_file = argv[itr+1]; itr++; } else { goto sempar; } } /* Arquivo de entrada */ else if( strcmp(argv[itr],"-e") == 0 || strcmp(argv[itr],"--entrada") == 0 || strcmp(argv[itr],"-i") == 0 || strcmp(argv[itr],"--input") == 0 ) { if(itr+1 < argc) { entrada_file = argv[itr+1]; itr++; } else { goto sempar; } } /* Arquivo de saida */ else if( strcmp(argv[itr],"-s") == 0 || strcmp(argv[itr],"--saida") == 0 || strcmp(argv[itr],"-o") == 0 || strcmp(argv[itr],"--output") == 0 ) { if(itr+1 < argc) { saida_file = argv[itr+1]; itr++; } else { goto sempar; } } /* Erro desconhecido */ else { if(rank == 0) { printf("Erro nos argumentos passados.\n"); } goto help; } } /* Fim da leitura dos argumentos */ if( operacao == INDEFINIDA || chave_file == NULL || entrada_file == NULL || saida_file == NULL ) { if(rank == 0) { if( operacao == INDEFINIDA ) printf("A operacao a ser realizada eh invalida ou nao foi especificada.\n"); if( chave_file == NULL ) printf("Esta faltando especificar o arquivo com a chave.\n"); if( entrada_file == NULL ) printf("Esta faltando especificar o arquivo de entrada.\n"); if( saida_file == NULL ) printf("Esta faltando especificar o arquivo de saida.\n"); } goto help; } /* Fim do tratamento dos argumentos */ if( MPI_File_open( MPI_COMM_WORLD, chave_file, MPI_MODE_RDONLY, MPI_INFO_NULL, &chave_handle ) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na abertura do arquivo com a chave (%s).\n",chave_file); } goto help; } if( MPI_File_read(chave_handle,&Nb,1, MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na leitura do tamanho de um bloco no arquivo com a chave (%s).\n",chave_file); } goto help; } if( Nb< 4 || Nb > 8 ) { if( rank == 0 ) { printf("Tamanho de bloco invalido no arquivo com a chave (%s).\n",chave_file); } goto help; } if( MPI_File_read(chave_handle,&Nk,1, MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na leitura do tamanho da chave no arquivo com a chave (%s).\n",chave_file); } goto help; } if( Nk< 4 || Nk > 8 ) { if( rank == 0 ) { printf("Tamanho de chave invalido no arquivo com a chave (%s).\n",chave_file); } goto help; } if( MPI_File_read(chave_handle,chave,4*Nk,MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na leitura da chave no arquivo com a chave (%s).\n",chave_file); } goto help; } MPI_File_close( &chave_handle ); Nr = numero_rodadas(Nb,Nk); KeyExpansion(chave,Nb,Nk); if( MPI_File_open( MPI_COMM_WORLD, entrada_file, MPI_MODE_RDONLY, MPI_INFO_NULL, &entrada_handle ) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na abertura do arquivo de entrada (%s).\n",entrada_file); } goto help; } MPI_File_get_size(entrada_handle,&entrada_bytes); if( MPI_File_open( MPI_COMM_WORLD, saida_file, MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_EXCL, MPI_INFO_NULL, &saida_handle ) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro na criacao do arquivo de saida (%s).\n",saida_file); printf("Uma possivel causa eh que o arquivo ja exista.\n"); } goto help; } numero_blocos = ( entrada_bytes / (Nb*4) ); blocos_processo = numero_blocos / worldsize; if( operacao == ENCRIPTAR || operacao == DECRIPTAR ) { bloco_byte_inicio = 4*Nb*blocos_processo*rank; bloco_byte_fim = 4*Nb*blocos_processo*(rank+1); tabela = novaTabela(Nb*4); for( iterador = bloco_byte_inicio ; iterador < bloco_byte_fim ; iterador += (4*Nb) ) { if( MPI_File_read_at(entrada_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao ler do arquivo de entrada (%s).\n",entrada_file); } goto help; } novaOcorrenciaTabela(tabela,bloco,iterador); } iterador = 4*Nb*blocos_processo*worldsize + 4*Nb*rank; if( iterador < numero_blocos*4*Nb ) { if( MPI_File_read_at(entrada_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao ler do arquivo de entrada (%s).\n",entrada_file); } goto help; } novaOcorrenciaTabela(tabela,bloco,iterador); } else if( operacao == ENCRIPTAR && iterador == numero_blocos*4*Nb ) { if( MPI_File_read_at(entrada_handle,iterador,bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao ler do arquivo de entrada (%s).\n",entrada_file); } goto help; } bloco[ 4*Nb - 1 ] = (octeto)(entrada_bytes - numero_blocos*4*Nb); novaOcorrenciaTabela(tabela,bloco,iterador); } if( juntar == 1 ) { tabelaEmpacotada = (octeto*)malloc( entrada_bytes ); if( rank == 0 ) /* Mestre que vai concatenar todas as arvores*/ { for(proc=1;proc<worldsize;proc++) { MPI_Recv( tabelaEmpacotada, entrada_bytes, MPI_BYTE, MPI_ANY_SOURCE, TAG_TABELA_EMPACOTADA, MPI_COMM_WORLD, &status ); desempacotarInserindo(tabelaEmpacotada,tabela); } tamanho_tabela = numeroBlocosTabela(tabela); no_proc = (tamanho_tabela / worldsize); no_resto = (tamanho_tabela % worldsize); tabela2 = novaTabela(Nb*4); for(proc=1;proc<worldsize;proc++) { for(i=0;i<no_proc;i++) { soInsiraTabela(tabela2, popLastTabelaNode(tabela) ); } if( no_resto > 1 ) { soInsiraTabela(tabela2, popLastTabelaNode(tabela) ); no_resto--; } empacotarTabela(tabela2,tabelaEmpacotada); MPI_Send(tabelaEmpacotada,numeroBytesTabela(tabela2), MPI_BYTE, proc, TAG_TABELA_EMPACOTADA_2, MPI_COMM_WORLD ); destruirArvore(tabela2->root); tabela2->root = NULL; } destruirTabela(tabela2); } else { empacotarTabela(tabela,tabelaEmpacotada); MPI_Send(tabelaEmpacotada,numeroBytesTabela(tabela), MPI_BYTE, 0, TAG_TABELA_EMPACOTADA, MPI_COMM_WORLD ); destruirArvore(tabela->root); tabela->root = NULL; MPI_Recv( tabelaEmpacotada, entrada_bytes, MPI_BYTE, 0, TAG_TABELA_EMPACOTADA_2, MPI_COMM_WORLD, &status ); desempacotarInserindo(tabelaEmpacotada,tabela); } free(tabelaEmpacotada); } if( operacao == ENCRIPTAR ) MPI_File_set_size(saida_handle,(MPI_Offset)( (numero_blocos+1)*(Nb*4) ) ); else if( operacao == DECRIPTAR ) MPI_File_set_size(saida_handle,entrada_bytes); tamanho_tabela = numeroBlocosTabela(tabela); for( i=0 ; i<tamanho_tabela ; i++ ) { node = popLastTabelaNode(tabela); // memcpy (bloco,node->bloco,4*Nb); if( operacao == ENCRIPTAR ) AES_encriptar_bloco(node->bloco,Nb,chave,Nr); else if( operacao == DECRIPTAR ) AES_decriptar_bloco(node->bloco,Nb,chave,Nr); indice = node->ocorrencias; while( indice != NULL ) { if( MPI_File_write_at(saida_handle,indice->indice,node->bloco,(4*Nb),MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao escrever no arquivo de saida (%s).\n",saida_file); } goto help; } indice = indice->next; } destruirArvore(node); } destruirTabela(tabela); if( operacao == DECRIPTAR ) { MPI_Barrier( MPI_COMM_WORLD ); /*Barreira q impede q alguem leia antes do valor decriptografado ser escrito */ if( MPI_File_read_at(saida_handle,entrada_bytes-1,bloco,1,MPI_BYTE,&status) != MPI_SUCCESS ) { if( rank == 0 ) { printf("Erro ao realizar leitura no arquivo de saida (%s).\n",saida_file); } goto help; } MPI_Barrier( MPI_COMM_WORLD ); /* Barreira q impede q alqum processo trunque o arquivo antes de outro processo ler*/ MPI_File_set_size(saida_handle,entrada_bytes - 4*Nb + bloco[0]); } if( rank == 0 && verbose==1) { if( operacao == ENCRIPTAR ) printf("A encriptacao do arquivo foi realizada com sucesso.\n"); else if( operacao == DECRIPTAR ) printf("A decriptacao do arquivo foi realizada com sucesso.\n"); } } goto finalizando; sempar: if( rank == 0 ) { printf("Sem par correspondente para a opcao %s.\n",argv[itr]); } help: if( rank == 0 ) { printf("Use a opcao --help para melhor entendimento do uso da aplicacao.\n"); } finalizando: MPI_Finalize( ); return 0; }
void mpi_file_set_size_f(MPI_Fint *fh, MPI_Offset *size, MPI_Fint *ierr) { MPI_File c_fh = MPI_File_f2c(*fh); *ierr = OMPI_INT_2_FINT(MPI_File_set_size(c_fh, (MPI_Offset) *size)); }