Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <mpi.h>
- #define DEBUG 1
- //Comandos para compilar y ejecutar
- //mpicc main.c -o main -lm
- //mpirun --hostfile hostfile -np 5 main
- //lamboot
- /* Translation of the DNA bases
- A -> 0
- C -> 1
- G -> 2
- T -> 3
- N -> 4*/
- #define M 10 // Number of sequences
- #define N 10 // Number of bases per sequence
- unsigned int g_seed = 0;
- int fast_rand(void) {
- g_seed = (214013*g_seed+2531011);
- return (g_seed>>16) % 5;
- }
- // The distance between two bases
- int base_distance(int base1, int base2){
- /*
- if((base1 == 4) || (base2 == 4)){
- return 3;
- }
- if(base1 == base2) {
- return 0;
- }
- if((base1 == 0) && (base2 == 3)) {
- return 1;
- }
- if((base2 == 0) && (base1 == 3)) {
- return 1;
- }
- if((base1 == 1) && (base2 == 2)) {
- return 1;
- }
- if((base2 == 2) && (base1 == 1)) {
- return 1;
- }*/
- return 2;
- }
- int main(int argc, char *argv[] ) {
- int i, j;
- int *data1, *data2;
- int *result;
- struct timeval tv1, tv2;
- int numprocs , rank , namelen;
- char processor_name [MPI_MAX_PROCESSOR_NAME];
- MPI_Init(&argc, &argv);
- MPI_Comm_size (MPI_COMM_WORLD , &numprocs);
- MPI_Comm_rank (MPI_COMM_WORLD , &rank);
- MPI_Get_processor_name (processor_name , &namelen);
- data1 = (int *) malloc(M*N*sizeof(int));
- data2 = (int *) malloc(M*N*sizeof(int));
- result = (int *) malloc(M*sizeof(int)); ///////////////////////////////////////////////////////////////////
- /* Initialize Matrices */
- if(rank == 0) {
- for (i = 0; i < M; i++) {
- for (j = 0; j < N; j++) {
- /* random with 20% gap proportion */
- data1[i * N + j] = fast_rand();
- data2[i * N + j] = fast_rand();
- }
- }
- }
- //int *recibir = malloc(sizeof(int) * (M*N)/numprocs);
- //int *recibir2 = malloc(sizeof(int) * (M*N)/numprocs);
- int *recibirv;
- int *recibirv2;
- int *recibirFinal = (int *) malloc(M*sizeof(int)); //////////////////////////////////////////////////////
- printf("LLEGA 0 y rank es : %d\n",rank);
- int resto = (M*N) % numprocs;
- int *sendcounts = malloc(sizeof(int)*numprocs);
- int *sendcounts2 = malloc(sizeof(int)*numprocs);
- int *displs = malloc(sizeof(int)*numprocs);
- int *displs2 = malloc(sizeof(int)*numprocs);
- int sum = 0;
- int longitudEnviar, longitudRecibir;
- //MPI_Scatter(data1, (M*N)/numprocs, MPI_INT, recibir, (M*N)/numprocs, MPI_INT, 0, MPI_COMM_WORLD);
- //MPI_Scatter(data2, (M*N)/numprocs, MPI_INT, recibir2, (M*N)/numprocs, MPI_INT, 0, MPI_COMM_WORLD);
- for (i = 0; i < numprocs; i++) {
- sendcounts[i] = (M*N)/numprocs;
- if (resto > 0) {
- sendcounts[i]++;
- resto--;
- }
- printf("rank %d y sendcounts[%d]: %d\n", rank, i, sendcounts[i]);
- displs[i] = sum;
- sum += sendcounts[i];
- }
- resto = (M*N) % numprocs;
- sum=0;
- for (i = 0; i < numprocs; i++) {
- sendcounts2[i] = M/numprocs;
- if (resto > 0) {
- sendcounts2[i]++;
- resto--;
- }
- printf("rank %d y sendcounts2[%d]: %d\n", rank, i, sendcounts2[i]);
- displs2[i] = sum;
- sum += sendcounts2[i];
- }
- longitudEnviar = M*N/numprocs;
- longitudRecibir = M/numprocs;
- if(rank< M*N % numprocs){ ///////////////////////////////¿M*N o M?
- longitudEnviar++;
- }
- if(rank< M % numprocs){ ///////////////////////////////¿M*N o M?
- longitudRecibir++;
- }
- recibirv = malloc(sizeof(int) * longitudEnviar);
- recibirv2 = malloc(sizeof(int) * longitudEnviar);
- //printf("Proceso %d y longitud %d\n", rank, longitud);
- MPI_Scatterv(data1, sendcounts, displs, MPI_INT, recibirv, longitudEnviar, MPI_INT, 0, MPI_COMM_WORLD); // Longitud¿?
- MPI_Scatterv(data2, sendcounts, displs, MPI_INT, recibirv2, longitudEnviar, MPI_INT, 0, MPI_COMM_WORLD);
- gettimeofday(&tv1, NULL);
- printf("LLEGA 1\n");
- if(longitudEnviar == (M*N)/numprocs) {
- for (i = 0; i < M / numprocs; i++) { ////////////////////////////////////
- result[i] = 0;
- for (j = 0; j < N; j++) {
- result[i] += base_distance(data1[i * N + j], data2[i * N + j]);
- }
- printf("rank es %d y result[%d] es %d\n",rank,i,result[i]);
- }
- }else{
- for (i = 0; i < M / numprocs+1; i++) { //////////////////////////////////// ¿+1?
- result[i] = 0;
- for (j = 0; j < N; j++) {
- result[i] += base_distance(data1[i * N + j], data2[i * N + j]);
- }
- printf("rank es %d y result[%d] es %d\n",rank,i,result[i]);
- }
- }
- printf("LLEGA 2\n");
- gettimeofday(&tv2, NULL);
- int microseconds = (tv2.tv_usec - tv1.tv_usec)+ 1000000 * (tv2.tv_sec - tv1.tv_sec);
- printf("Antes gatherv\n");
- MPI_Gatherv(result, longitudRecibir, MPI_INT, recibirFinal, sendcounts2, displs2, MPI_INT, 0, MPI_COMM_WORLD);
- //MPI_Gather (result, M/numprocs, MPI_INT, recibirFinal, M/numprocs, MPI_INT, 0, MPI_COMM_WORLD);
- printf("Después gatherv\n");
- printf("Llega3\n");
- /* Display result */
- if (DEBUG == 1) {
- if(rank==0) {
- int checksum = 0;
- for (i = 0; i < M; i++) {
- printf("recibirFinal[%d] es: %d\n", i,recibirFinal[i]);
- checksum += recibirFinal[i];
- }
- printf("Checksum: %d\n ", checksum);
- }
- } else if (DEBUG == 2) {
- if(rank==0) {
- for (i = 0; i < M; i++) {
- printf(" %d \t ", recibirFinal[i]);
- }
- }
- } else {
- printf ("Time (seconds) = %lf\n", (double) microseconds/1E6);
- }
- printf("LLEGA 4\n");
- free(data1); free(data2); free(result); free(recibirFinal); free(recibirv); free(recibirv2);
- MPI_Finalize();
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement