|
| 1 | +/* |
| 2 | + * Copyright 2014, Simon Pickartz Institute for Automation |
| 3 | + * of Complex Power Systems, |
| 4 | + * RWTH Aachen University |
| 5 | + * |
| 6 | + * Based on pingpong_length by Carsten Clauss, Chair for Operating Systems |
| 7 | + * RWTH Aachen University |
| 8 | + * |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +#include <string.h> |
| 24 | +#include <stdbool.h> |
| 25 | +#include <stdio.h> |
| 26 | +#include <unistd.h> |
| 27 | +#include <stdint.h> |
| 28 | +#include <stdlib.h> |
| 29 | +#include <time.h> |
| 30 | +#include <errno.h> |
| 31 | + |
| 32 | +#include <mpi.h> |
| 33 | + |
| 34 | +#include <stat_eval.h> |
| 35 | + |
| 36 | +#undef _WATCH_DOG_ |
| 37 | +#define _CACHE_WARM_UP_ |
| 38 | +#undef _USE_SEPARATED_BUFFERS_ |
| 39 | +#undef _PRINT_INDIVIDUAL_RES_ |
| 40 | + |
| 41 | +#define MAXBUFSIZE (64) |
| 42 | +#define DEFAULTLEN (0) |
| 43 | +#define DEFAULTROUNDS (10000) |
| 44 | +#define DEFAULTITER (1) |
| 45 | +#define WARMUPITER (10000) |
| 46 | + |
| 47 | +#ifdef _USE_SEPARATED_BUFFERS_ |
| 48 | +unsigned char send_buffer[MAXBUFSIZE+1]; |
| 49 | +unsigned char recv_buffer[MAXBUFSIZE+1]; |
| 50 | +#else |
| 51 | +#define send_buffer buffer |
| 52 | +#define recv_buffer buffer |
| 53 | +unsigned char buffer[MAXBUFSIZE+1]; |
| 54 | +#endif |
| 55 | +unsigned char dummy = 0; |
| 56 | + |
| 57 | +int main(int argc, char **argv) |
| 58 | +{ |
| 59 | + int arg; |
| 60 | + uint32_t i; |
| 61 | + int32_t num_ranks; |
| 62 | + int32_t remote_rank, my_rank; |
| 63 | + |
| 64 | + uint32_t length = DEFAULTLEN; |
| 65 | + uint32_t iterations = DEFAULTITER; |
| 66 | + int32_t numrounds = DEFAULTROUNDS; |
| 67 | + int32_t round; |
| 68 | + |
| 69 | + double timer; |
| 70 | + double *time_stamps = NULL; |
| 71 | + stat_eval_t stat_eval; |
| 72 | + bool run_infinitely; |
| 73 | + MPI_Status status; |
| 74 | + char *filename = NULL; |
| 75 | + |
| 76 | + /* determine arguments */ |
| 77 | + while ((arg = getopt(argc, argv, "i:r:l:hf:")) != -1) { |
| 78 | + switch (arg) { |
| 79 | + case 'r': |
| 80 | + numrounds = atoi(optarg); |
| 81 | + break; |
| 82 | + case 'f': |
| 83 | + filename = optarg; |
| 84 | + break; |
| 85 | + case 'l': |
| 86 | + length = atoi(optarg); |
| 87 | + break; |
| 88 | + case 'i': |
| 89 | + iterations = atoi(optarg); |
| 90 | + break; |
| 91 | + case 'h': |
| 92 | + printf("usage %s [-l message_length (def: %d)] " |
| 93 | + "[-i iterations (def: %d)] " |
| 94 | + "[-r rounds (def: %d)] " |
| 95 | + "[-f filename]\n", |
| 96 | + argv[0], |
| 97 | + DEFAULTLEN, |
| 98 | + DEFAULTITER, |
| 99 | + DEFAULTROUNDS); |
| 100 | + exit(0); |
| 101 | + |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /* initialize MPI environment */ |
| 106 | + MPI_Init(&argc, &argv); |
| 107 | + MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); |
| 108 | + MPI_Comm_size(MPI_COMM_WORLD, &num_ranks); |
| 109 | + |
| 110 | + |
| 111 | + /* check for errors and determine remote rank */ |
| 112 | + if (num_ranks != 2) { |
| 113 | + if(my_rank == 0) |
| 114 | + fprintf(stderr, |
| 115 | + "Pingpong needs exactly two UEs; try again\n"); |
| 116 | + exit(-1); |
| 117 | + } |
| 118 | + remote_rank = (my_rank+1)%2; |
| 119 | + |
| 120 | + |
| 121 | + /* perform a warm-up of the cache */ |
| 122 | +#ifdef _CACHE_WARM_UP_ |
| 123 | + for(i=0; i < length; i++) |
| 124 | + { |
| 125 | + /* cache warm-up: */ |
| 126 | + dummy += send_buffer[i]; |
| 127 | + dummy += recv_buffer[i]; |
| 128 | + } |
| 129 | +#endif |
| 130 | + |
| 131 | + /* check for infinite test */ |
| 132 | + if (numrounds == -1) { |
| 133 | + run_infinitely = true; |
| 134 | + } else { |
| 135 | + run_infinitely = false; |
| 136 | + time_stamps = (double*)calloc(sizeof(double), numrounds); |
| 137 | + } |
| 138 | + |
| 139 | + if (my_rank == 0) { |
| 140 | + printf("Starting the benchmark:\n"); |
| 141 | + if (numrounds == -1) { |
| 142 | + printf("Rounds : inf\n"); |
| 143 | + } else { |
| 144 | + printf("Rounds : %10d\n", numrounds); |
| 145 | + } |
| 146 | + printf("Iterations : %10d\n", iterations); |
| 147 | + printf("Msg Length : %10d\n", length); |
| 148 | + if (filename) { |
| 149 | + printf("Filename : %s\n", filename); |
| 150 | + } else { |
| 151 | + printf("Filename : stdout\n"); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /* synchronize and start the PingPong */ |
| 156 | + MPI_Barrier(MPI_COMM_WORLD); |
| 157 | + if(my_rank == 0) { |
| 158 | + for (i=0; i<WARMUPITER; ++i) { |
| 159 | + MPI_Send(send_buffer, |
| 160 | + length, |
| 161 | + MPI_CHAR, |
| 162 | + remote_rank, |
| 163 | + 0, |
| 164 | + MPI_COMM_WORLD); |
| 165 | + MPI_Recv(recv_buffer, |
| 166 | + length, |
| 167 | + MPI_CHAR, |
| 168 | + remote_rank, |
| 169 | + 0, |
| 170 | + MPI_COMM_WORLD, |
| 171 | + &status); |
| 172 | + } |
| 173 | + MPI_Barrier(MPI_COMM_WORLD); |
| 174 | + |
| 175 | + for (round = 0; run_infinitely || (round < numrounds); ++round) { |
| 176 | + /* start timer: */ |
| 177 | + timer = MPI_Wtime(); |
| 178 | + |
| 179 | + for (i=0; i<iterations; ++i) { |
| 180 | + MPI_Send(send_buffer, |
| 181 | + length, |
| 182 | + MPI_CHAR, |
| 183 | + remote_rank, |
| 184 | + 0, |
| 185 | + MPI_COMM_WORLD); |
| 186 | + MPI_Recv(recv_buffer, |
| 187 | + length, |
| 188 | + MPI_CHAR, |
| 189 | + remote_rank, |
| 190 | + 0, |
| 191 | + MPI_COMM_WORLD, |
| 192 | + &status); |
| 193 | + } |
| 194 | + |
| 195 | + /* stop timer: */ |
| 196 | + timer = (MPI_Wtime() - timer); |
| 197 | + if (run_infinitely == false) |
| 198 | + time_stamps[round] = timer*1e6/(2*iterations); |
| 199 | +#ifdef _PRINT_INDIVIDUAL_RES_ |
| 200 | + printf("%d\t\t%1.2lf\t\t%1.2lf\n", |
| 201 | + length, |
| 202 | + timer/(2.0*iterations)*1000000, |
| 203 | + (length/(timer/(2.0*iterations)))/(1024*1024)); |
| 204 | + fflush(stdout); |
| 205 | + |
| 206 | +#endif |
| 207 | +#ifdef _WATCH_DOG_ |
| 208 | + if (!(round%100000)) |
| 209 | + printf("Round %d ...\n", round); |
| 210 | +#endif |
| 211 | + } |
| 212 | + } else { |
| 213 | + for (i=0; i<WARMUPITER; ++i) { |
| 214 | + MPI_Recv(recv_buffer, |
| 215 | + length, |
| 216 | + MPI_CHAR, |
| 217 | + remote_rank, |
| 218 | + 0, |
| 219 | + MPI_COMM_WORLD, |
| 220 | + &status); |
| 221 | + MPI_Send(send_buffer, |
| 222 | + length, |
| 223 | + MPI_CHAR, |
| 224 | + remote_rank, |
| 225 | + 0, |
| 226 | + MPI_COMM_WORLD); |
| 227 | + } |
| 228 | + MPI_Barrier(MPI_COMM_WORLD); |
| 229 | + |
| 230 | + for (round = 0; run_infinitely || (round < numrounds); ++round) { |
| 231 | + for (i=0; i<iterations; ++i) { |
| 232 | + MPI_Recv(recv_buffer, |
| 233 | + length, |
| 234 | + MPI_CHAR, |
| 235 | + remote_rank, |
| 236 | + 0, |
| 237 | + MPI_COMM_WORLD, |
| 238 | + &status); |
| 239 | + MPI_Send(send_buffer, |
| 240 | + length, |
| 241 | + MPI_CHAR, |
| 242 | + remote_rank, |
| 243 | + 0, |
| 244 | + MPI_COMM_WORLD); |
| 245 | + } |
| 246 | + |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + /* Statistical evaluation */ |
| 251 | + if ((my_rank == 0) && (run_infinitely == false)) { |
| 252 | + statistical_eval(time_stamps, |
| 253 | + numrounds, |
| 254 | + &stat_eval); |
| 255 | + |
| 256 | + /* print the results */ |
| 257 | + FILE *output = stdout; |
| 258 | + if (filename) { |
| 259 | + output = fopen(filename, "w+"); |
| 260 | + } |
| 261 | + print_statistics(&stat_eval, numrounds, output); |
| 262 | + if (filename) { |
| 263 | + fclose(output); |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + MPI_Finalize(); |
| 268 | + |
| 269 | + return 0; |
| 270 | +} |
| 271 | + |
0 commit comments