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,
96 MPI_Send(vector, N, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
101 if (i != size || j != N) {
102 printf("index calc error: i = %d, size = %d, j = %d, N = %d\n",
104 MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
109 vcalc = (double *) malloc(N * sizeof(double));
113 matrix = (double *) malloc(N * n * sizeof(double));
114 vector = (double *) malloc(N * sizeof(double));
115 vcalc = (double *) malloc(n * sizeof(double));
117 MPI_Recv(matrix, N * n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
118 MPI_Recv(vector, N, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
122 for (i = 0; i < n; i++) {
124 for (j = 0; j < N; j++) {
125 vcalc[i] += matrix[N * i + j] * vector[j];
130 MPI_Send(vcalc, n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
133 for (i = 1, j = n; i < size && j < N; i++, j += k) {
134 k = N / size + ((i < (N % size)) ? 1 : 0);
135 MPI_Recv(vcalc + j, k, MPI_DOUBLE, i, 0, MPI_COMM_WORLD,
141 if (i != size || j != N) {
142 printf("index calc error 2: i = %d, size = %d, j = %d, N = %d\n",
144 MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
149 if (-1 == gettimeofday(stop_time, NULL)) {
150 printf("couldn't set stop_time on node 0!\n");
151 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
152 exit(GETTIMEOFDAY_ERROR);
156 (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
157 (start_time->tv_sec * 1000000 + start_time->tv_usec);
159 if (-1 == gettimeofday(start_time, NULL)) {
160 printf("couldn't set start_time on node 0!\n");
161 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
162 exit(GETTIMEOFDAY_ERROR);
164 // calculate serially
165 vcheck = (double *) malloc(N * sizeof(double));
166 for (i = 0; i < N; i++) {
168 for (j = 0; j < N; j++) {
169 vcheck[i] += matrix[N * i + j] * vector[j];
173 if (-1 == gettimeofday(stop_time, NULL)) {
174 printf("couldn't set stop_time on node 0!\n");
175 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
176 exit(GETTIMEOFDAY_ERROR);
180 (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
181 (start_time->tv_sec * 1000000 + start_time->tv_usec);
183 // verify correctness
184 for (i = 0; i < N && vcalc[i] == vcheck[i]; i++);
186 printf("prog: blocking, i: %d ", current_iteration);
190 ("ptime: %ld us, stime: %ld us, speedup: %.3f, nodes: %d, efficiency: %.3f\n",
191 parallel_usecs, sequential_usecs,
192 (double) sequential_usecs / (double) parallel_usecs, size,
193 (double) sequential_usecs / ((double) parallel_usecs *
196 parallel_usecs_total += parallel_usecs;
197 sequential_usecs_total += sequential_usecs;
198 successful_iterations++;
200 printf("parallel calc != serial calc, ");
215 printf("prog: blocking, ");
216 if (0 < successful_iterations) {
218 ("iterations: %d, avg. ptime: %.3f us, avg. stime: %.3f us, avg. speedup: %.3f, nodes: %d, avg. efficiency: %.3f\n",
219 successful_iterations,
220 (double) parallel_usecs_total / (double) successful_iterations,
221 (double) sequential_usecs_total /
222 (double) successful_iterations,
223 (double) sequential_usecs_total / (double) parallel_usecs_total,
225 (double) sequential_usecs_total /
226 ((double) parallel_usecs_total * (double) size));
228 printf("no successful iterations!\n");