File: mpi_non_blocking.c

package info (click to toggle)
openmx 3.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 325,856 kB
  • ctags: 3,575
  • sloc: ansic: 152,655; f90: 2,080; python: 876; makefile: 675; sh: 25; perl: 18
file content (154 lines) | stat: -rw-r--r-- 2,601 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/times.h>
#include "mpi.h"



static void dtime(double *t);
static int rnd(int numprocs);


int main(int argc, char *argv[]) 
{
  int numprocs,myid,ID;
  int i,NN; 
  int *Num_Snd,*Num_Rcv;

  double **SndA;
  double **RcvA;
  double TStime,TEtime;
  double sum;

  MPI_Status stat;
  MPI_Request request;

  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD,&myid);

  NN = 3;

  /* allocation of arrays */
  
  Num_Snd = (int*)malloc(sizeof(int)*numprocs);
  Num_Rcv = (int*)malloc(sizeof(int)*numprocs);
  
  /* set data */

  for (ID=0; ID<numprocs; ID++){
    Num_Snd[ID] = 0; 
    Num_Rcv[ID] = 0; 
  }

  for (i=0; i<NN; i++){
    printf("%3d\n",rnd(numprocs));  
  }  



  
  /* 
    First case:
    MPI_Isend and MPI_Recv 
  */






  /* 
    Second case:
    MPI_Isend and MPI_Irecv 
  */


  /*
  SndA = (double**)malloc(sizeof(double*)*numprocs);
  for (i=0; i<numprocs; i++){
    SndA[i] = (double*)malloc(sizeof(double)*asize1);
  }

  RcvA = (double**)malloc(sizeof(double*)*numprocs);
  for (i=0; i<numprocs; i++){
    RcvA[i] = (double*)malloc(sizeof(double)*asize1);
  }


  printf("numprocs=%2d myid=%2d\n",numprocs,myid);

  for (i=0; i<asize1; i++){
    SndA[myid][i] = cos((double)myid*(double)i);
  }

  tag = 999;
 
  MPI_Barrier(mpi_comm_level1);
  dtime(&TStime);
  */

  /*
  for (ID=0; ID<numprocs; ID++){
    if (ID!=myid){
      MPI_Isend(&SndA[myid][0], asize1, MPI_DOUBLE, ID, tag, mpi_comm_level1, &request);
      MPI_Recv( &RcvA[ID][0], asize1, MPI_DOUBLE, ID, tag, mpi_comm_level1, &stat);
      MPI_Wait(&request,&stat);
    }
  }
  */

  /*
  for (ID=0; ID<numprocs; ID++){
    if (ID!=myid){
      MPI_Isend(&SndA[myid][0], asize1, MPI_DOUBLE, ID, tag, mpi_comm_level1, &request);
    }
  }
  for (ID=0; ID<numprocs; ID++){
    if (ID!=myid){
      MPI_Recv( &RcvA[ID][0], asize1, MPI_DOUBLE, ID, tag, mpi_comm_level1, &stat);
    }
  }
  MPI_Wait(&request,&stat);


  MPI_Barrier(mpi_comm_level1);
  dtime(&TEtime);

  sum = 0.0;
  for (i=0; i<asize1; i++){
    sum += RcvA[1][i];
  }
  printf("myid=%2d elapsed time = %15.10f sum=%15.12f\n",myid,TEtime-TStime,sum);
  */

  MPI_Finalize();
}


void dtime(double *t)
{

  // real time
  struct timeval timev;
  gettimeofday(&timev, NULL);
  *t = timev.tv_sec + (double)timev.tv_usec*1e-6;

}


int rnd(int numprocs)
{
  double result;
  result = rand();
  while (2.0<result){
    result = result/2.0;
  }

  return (int)result;
}