10 #define GETTIMEOFDAY_ERROR 3
12 int main(int argc, char *argv[])
16 int N, n, i, j, k, current_iteration, successful_iterations = 0;
17 double *matrix = NULL, *vector = NULL, *vcalc, *vcheck;
19 struct timeval *start_time = NULL, *stop_time = NULL;
20 long parallel_usecs, parallel_usecs_total =
21 0, sequential_usecs, sequential_usecs_total = 0;
23 MPI_Init(&argc, &argv);
25 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
26 MPI_Comm_size(MPI_COMM_WORLD, &size);
30 // root node parses cmdline args
31 if (2 > argc || !isdigit(*argv[1])) {
32 printf("usage:\n%s <size>\n", argv[0]);
33 MPI_Abort(MPI_COMM_WORLD, USAGE_ERROR);
39 start_time = (struct timeval *) malloc(sizeof(struct timeval));
40 stop_time = (struct timeval *) malloc(sizeof(struct timeval));
44 for (current_iteration = 0; current_iteration < ITERATIONS;
45 current_iteration++) {
49 matrix = (double *) malloc(N * N * sizeof(double));
50 vector = (double *) malloc(N * sizeof(double));
52 for (i = 0; i < N * N; i++) {
53 matrix[i] = (double) rand() / ((double) RAND_MAX + 1);
56 for (i = 0; i < N; i++) {
57 vector[i] = (double) rand() / ((double) RAND_MAX + 1);
60 // for the sake of argument, the parallel algorithm begins
61 // when the root node begins to transmit the matrix to the
63 if (-1 == gettimeofday(start_time, NULL)) {
64 printf("couldn't set start_time on node 0!\n");
65 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
66 exit(GETTIMEOFDAY_ERROR);
69 for (i = 1; i < size; i++) {
70 MPI_Send(&N, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
74 MPI_Recv(&N, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
77 // this algorithm uses at most N processors...
82 n = N / size + ((rank < (N % size)) ? 1 : 0);
86 for (i = 1, j = n; i < size && j < N; i++, j += k) {
87 k = N / size + ((i < (N % size)) ? 1 : 0);
88 MPI_Send(matrix + N * j, N * k, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
89 MPI_Send(vector, N, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
94 if (i != size || j != N) {
95 printf("index calc error: i = %d, size = %d, j = %d, N = %d\n", i,
97 MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
102 vcalc = (double *) malloc(N * sizeof(double));
106 matrix = (double *) malloc(N * n * sizeof(double));
107 vector = (double *) malloc(N * sizeof(double));
108 vcalc = (double *) malloc(n * sizeof(double));
110 MPI_Recv(matrix, N * n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
111 MPI_Recv(vector, N, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
115 for (i = 0; i < n; i++) {
117 for (j = 0; j < N; j++) {
118 vcalc[i] += matrix[N * i + j] * vector[j];
123 MPI_Send(vcalc, n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
126 for (i = 1, j = n; i < size && j < N; i++, j += k) {
127 k = N / size + ((i < (N % size)) ? 1 : 0);
128 MPI_Recv(vcalc + j, k, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, &status);
133 if (i != size || j != N) {
134 printf("index calc error 2: i = %d, size = %d, j = %d, N = %d\n", i,
136 MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
141 if (-1 == gettimeofday(stop_time, NULL)) {
142 printf("couldn't set stop_time on node 0!\n");
143 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
144 exit(GETTIMEOFDAY_ERROR);
148 (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
149 (start_time->tv_sec * 1000000 + start_time->tv_usec);
151 if (-1 == gettimeofday(start_time, NULL)) {
152 printf("couldn't set start_time on node 0!\n");
153 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
154 exit(GETTIMEOFDAY_ERROR);
156 // calculate serially
157 vcheck = (double *) malloc(N * sizeof(double));
158 for (i = 0; i < N; i++) {
160 for (j = 0; j < N; j++) {
161 vcheck[i] += matrix[N * i + j] * vector[j];
165 if (-1 == gettimeofday(stop_time, NULL)) {
166 printf("couldn't set stop_time on node 0!\n");
167 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
168 exit(GETTIMEOFDAY_ERROR);
172 (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
173 (start_time->tv_sec * 1000000 + start_time->tv_usec);
175 // verify correctness
176 for (i = 0; i < N && vcalc[i] == vcheck[i]; i++);
178 printf("prog: blocking, i: %d ", current_iteration);
182 ("ptime: %ld us, stime: %ld us, speedup: %.3f, nodes: %d, efficiency: %.3f\n",
183 parallel_usecs, sequential_usecs,
184 (double) sequential_usecs / (double) parallel_usecs, size,
185 (double) sequential_usecs / ((double) parallel_usecs *
188 parallel_usecs_total += parallel_usecs;
189 sequential_usecs_total += sequential_usecs;
190 successful_iterations++;
192 printf("parallel calc != serial calc, ");
207 printf("prog: blocking, ");
208 if (0 < successful_iterations) {
210 ("iterations: %d, avg. ptime: %.3f us, avg. stime: %.3f us, avg. speedup: %.3f, nodes: %d, avg. efficiency: %.3f\n",
211 successful_iterations,
212 (double) parallel_usecs_total / (double) successful_iterations,
213 (double) sequential_usecs_total / (double) successful_iterations,
214 (double) sequential_usecs_total / (double) parallel_usecs_total,
216 (double) sequential_usecs_total / ((double) parallel_usecs_total *
219 printf("no successful iterations!\n");