1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #define SANITY_ERROR 2
16 #define GETTIMEOFDAY_ERROR 3
18 int main(int argc, char *argv[])
22 int N, n, i, j, k, current_iteration, successful_iterations = 0;
23 double *matrix = NULL, *vector = NULL, *vcalc, *vcheck;
25 struct timeval *start_time = NULL, *stop_time = NULL;
26 long parallel_usecs, parallel_usecs_total =
27 0, sequential_usecs, sequential_usecs_total = 0;
29 MPI_Init(&argc, &argv);
31 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
32 MPI_Comm_size(MPI_COMM_WORLD, &size);
36 // root node parses cmdline args
37 if (2 > argc || !isdigit(*argv[1])) {
38 printf("usage:\n%s <size>\n", argv[0]);
39 MPI_Abort(MPI_COMM_WORLD, USAGE_ERROR);
45 start_time = (struct timeval *) malloc(sizeof(struct timeval));
46 stop_time = (struct timeval *) malloc(sizeof(struct timeval));
50 for (current_iteration = 0; current_iteration < ITERATIONS;
51 current_iteration++) {
55 matrix = (double *) malloc(N * N * sizeof(double));
56 vector = (double *) malloc(N * sizeof(double));
58 for (i = 0; i < N * N; i++) {
59 matrix[i] = (double) rand() / ((double) RAND_MAX + 1);
62 for (i = 0; i < N; i++) {
63 vector[i] = (double) rand() / ((double) RAND_MAX + 1);
66 // for the sake of argument, the parallel algorithm begins
67 // when the root node begins to transmit the matrix to the
69 if (-1 == gettimeofday(start_time, NULL)) {
70 printf("couldn't set start_time on node 0!\n");
71 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
72 exit(GETTIMEOFDAY_ERROR);
75 for (i = 1; i < size; i++) {
76 MPI_Send(&N, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
80 MPI_Recv(&N, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
83 // this algorithm uses at most N processors...
88 n = N / size + ((rank < (N % size)) ? 1 : 0);
92 for (i = 1, j = n; i < size && j < N; i++, j += k) {
93 k = N / size + ((i < (N % size)) ? 1 : 0);
94 MPI_Send(matrix + N * j, N * k, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
95 MPI_Send(vector, N, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
100 if (i != size || j != N) {
101 printf("index calc error: i = %d, size = %d, j = %d, N = %d\n", i,
103 MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
108 vcalc = (double *) malloc(N * sizeof(double));
112 matrix = (double *) malloc(N * n * sizeof(double));
113 vector = (double *) malloc(N * sizeof(double));
114 vcalc = (double *) malloc(n * sizeof(double));
116 MPI_Recv(matrix, N * n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
117 MPI_Recv(vector, N, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
121 for (i = 0; i < n; i++) {
123 for (j = 0; j < N; j++) {
124 vcalc[i] += matrix[N * i + j] * vector[j];
129 MPI_Send(vcalc, n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
132 for (i = 1, j = n; i < size && j < N; i++, j += k) {
133 k = N / size + ((i < (N % size)) ? 1 : 0);
134 MPI_Recv(vcalc + j, k, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, &status);
139 if (i != size || j != N) {
140 printf("index calc error 2: i = %d, size = %d, j = %d, N = %d\n", i,
142 MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
147 if (-1 == gettimeofday(stop_time, NULL)) {
148 printf("couldn't set stop_time on node 0!\n");
149 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
150 exit(GETTIMEOFDAY_ERROR);
154 (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
155 (start_time->tv_sec * 1000000 + start_time->tv_usec);
157 if (-1 == gettimeofday(start_time, NULL)) {
158 printf("couldn't set start_time on node 0!\n");
159 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
160 exit(GETTIMEOFDAY_ERROR);
162 // calculate serially
163 vcheck = (double *) malloc(N * sizeof(double));
164 for (i = 0; i < N; i++) {
166 for (j = 0; j < N; j++) {
167 vcheck[i] += matrix[N * i + j] * vector[j];
171 if (-1 == gettimeofday(stop_time, NULL)) {
172 printf("couldn't set stop_time on node 0!\n");
173 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
174 exit(GETTIMEOFDAY_ERROR);
178 (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
179 (start_time->tv_sec * 1000000 + start_time->tv_usec);
181 // verify correctness
182 for (i = 0; i < N && vcalc[i] == vcheck[i]; i++);
184 printf("prog: blocking, i: %d ", current_iteration);
188 ("ptime: %ld us, stime: %ld us, speedup: %.3f, nodes: %d, efficiency: %.3f\n",
189 parallel_usecs, sequential_usecs,
190 (double) sequential_usecs / (double) parallel_usecs, size,
191 (double) sequential_usecs / ((double) parallel_usecs *
194 parallel_usecs_total += parallel_usecs;
195 sequential_usecs_total += sequential_usecs;
196 successful_iterations++;
198 printf("parallel calc != serial calc, ");
213 printf("prog: blocking, ");
214 if (0 < successful_iterations) {
216 ("iterations: %d, avg. ptime: %.3f us, avg. stime: %.3f us, avg. speedup: %.3f, nodes: %d, avg. efficiency: %.3f\n",
217 successful_iterations,
218 (double) parallel_usecs_total / (double) successful_iterations,
219 (double) sequential_usecs_total / (double) successful_iterations,
220 (double) sequential_usecs_total / (double) parallel_usecs_total,
222 (double) sequential_usecs_total / ((double) parallel_usecs_total *
225 printf("no successful iterations!\n");