00001 #ifndef MPIB_COLL_CONTAINERS_HPP_
00002 #define MPIB_COLL_CONTAINERS_HPP_
00003
00004 #include "mpib_coll_benchmarks.h"
00005 #include <malloc.h>
00006 #include "mpib_defs.h"
00007
00027 class MPIB_buffer_container: public MPIB_coll_container {
00028 protected:
00029 char* buffer;
00030
00031 public:
00032 MPIB_buffer_container() {
00033 MPIB_coll_container::operation = "";
00034 MPIB_coll_container::finalize = finalize;
00035 }
00036
00037 static int finalize(void* _this, MPI_Comm comm, int root) {
00038 MPIB_buffer_container* container = (MPIB_buffer_container*)_this;
00039 free(container->buffer);
00040 return 0;
00041 }
00042 };
00043
00045 class MPIB_SG_container: public MPIB_buffer_container {
00046 public:
00047 MPIB_SG_container() {
00048 MPIB_coll_container::initialize = initialize;
00049 }
00050
00051 static int initialize(void* _this, MPI_Comm comm, int root, int M) {
00052 MPIB_SG_container* container = (MPIB_SG_container*)_this;
00053 int rank;
00054 MPI_Comm_rank(comm, &rank);
00055 int size;
00056 MPI_Comm_size(comm, &size);
00057 container->buffer = rank == root ?
00058 (char*)malloc(sizeof(char) * M * size) :
00059 (char*)malloc(sizeof(char) * M);
00060 return 0;
00061 }
00062 };
00063
00065 class MPIB_Scatter_container: public MPIB_SG_container {
00066 private:
00067 MPIB_Scatter scatter;
00068
00069 public:
00070 MPIB_Scatter_container(MPIB_Scatter scatter) {
00071 MPIB_coll_container::operation = "Scatter";
00072 MPIB_coll_container::execute = execute;
00073 this->scatter = scatter;
00074 }
00075
00076 static int execute(void* _this, MPI_Comm comm, int root, int M) {
00077 MPIB_Scatter_container* container = (MPIB_Scatter_container*)_this;
00078 return container->scatter(container->buffer, M, MPI_CHAR,
00079 container->buffer, M, MPI_CHAR,
00080 root, comm);
00081 }
00082 };
00083
00085 class MPIB_Gather_container: public MPIB_SG_container {
00086 private:
00087 MPIB_Gather gather;
00088
00089 public:
00090 MPIB_Gather_container(MPIB_Gather gather) {
00091 MPIB_coll_container::operation = "Gather";
00092 MPIB_coll_container::execute = execute;
00093 this->gather = gather;
00094 }
00095
00096 static int execute(void* _this, MPI_Comm comm, int root, int M) {
00097 MPIB_Gather_container* container = (MPIB_Gather_container*)_this;
00098 return container->gather(container->buffer, M, MPI_CHAR,
00099 container->buffer, M, MPI_CHAR,
00100 root, comm);
00101 }
00102 };
00103
00105 class MPIB_Bcast_container: public MPIB_buffer_container {
00106 private:
00107 MPIB_Bcast bcast;
00108
00109 public:
00110 MPIB_Bcast_container(MPIB_Bcast bcast) {
00111 MPIB_coll_container::operation = "Bcast";
00112 MPIB_coll_container::initialize = initialize;
00113 MPIB_coll_container::execute = execute;
00114 this->bcast = bcast;
00115 }
00116
00117 static int initialize(void* _this, MPI_Comm comm, int root, int M) {
00118 MPIB_Bcast_container* container = (MPIB_Bcast_container*)_this;
00119 container->buffer = (char*)malloc(sizeof(char) * M);
00120 return 0;
00121 }
00122
00123 static int execute(void* _this, MPI_Comm comm, int root, int M) {
00124 MPIB_Bcast_container* container = (MPIB_Bcast_container*)_this;
00125 return container->bcast(container->buffer, M, MPI_CHAR, root, comm);
00126 }
00127 };
00128
00130 class MPIB_Reduce_container: public MPIB_coll_container {
00131 private:
00132 MPIB_Reduce reduce;
00133 char* sendbuf;
00134 char* recvbuf;
00135
00136 public:
00137 MPIB_Reduce_container(MPIB_Reduce reduce) {
00138 MPIB_coll_container::operation = "Reduce";
00139 MPIB_coll_container::initialize = initialize;
00140 MPIB_coll_container::execute = execute;
00141 MPIB_coll_container::finalize = finalize;
00142 this->reduce = reduce;
00143 }
00144
00145 static int initialize(void* _this, MPI_Comm comm, int root, int M) {
00146 MPIB_Reduce_container* container = (MPIB_Reduce_container*)_this;
00147 container->sendbuf = (char*)malloc(sizeof(char) * M);
00148 int rank;
00149 MPI_Comm_rank(comm, &rank);
00150 container->recvbuf = rank == root ? (char*)malloc(sizeof(char) * M) : NULL;
00151 return 0;
00152 }
00153
00154 static int execute(void* _this, MPI_Comm comm, int root, int M) {
00155 MPIB_Reduce_container* container = (MPIB_Reduce_container*)_this;
00156 return container->reduce(container->sendbuf, container->recvbuf, M, MPI_CHAR, MPI_MAX, root, comm);
00157 }
00158
00159 static int finalize(void* _this, MPI_Comm comm, int root) {
00160 MPIB_Reduce_container* container = (MPIB_Reduce_container*)_this;
00161 free(container->sendbuf);
00162 free(container->recvbuf);
00163 return 0;
00164 }
00165 };
00166
00168 class MPIB_Comm_dup_free_container: public MPIB_coll_container {
00169 public:
00170 MPIB_Comm_dup_free_container() {
00171 MPIB_coll_container::operation = "MPI_Comm_dup-MPI_Comm_free";
00172 MPIB_coll_container::execute = execute;
00173 }
00174
00175 static int execute(void* _this, MPI_Comm comm, int root, int M) {
00176 MPI_Comm newcomm;
00177 MPI_Comm_dup(comm, &newcomm);
00178 MPI_Comm_free(&newcomm);
00179 return 0;
00180 }
00181 };
00182
00184 class MPIB_SGv_container: public MPIB_buffer_container {
00185 protected:
00186 const double* factors;
00187 int count;
00188 int* counts;
00189 int* displs;
00190
00191 public:
00192 MPIB_SGv_container(const double* _factors): factors(_factors) {
00193 MPIB_coll_container::initialize = initialize;
00194 MPIB_coll_container::finalize = finalize;
00195 }
00196
00197 static int initialize(void* _this, MPI_Comm comm, int root, int M) {
00198 MPIB_SGv_container* container = (MPIB_SGv_container*)_this;
00199 int count = 0;
00200 int size;
00201 MPI_Comm_size(comm, &size);
00202 container->counts = (int*)malloc(sizeof(int) * size);
00203 container->displs = (int*)malloc(sizeof(int) * size);
00204 for (int i = 0; i < size; i++) {
00205 count += container->counts[i] = M * container->factors[i];
00206 container->displs[i] = i == 0 ? 0 : container->displs[i - 1] + container->counts[i - 1];
00207 }
00208 int rank;
00209 MPI_Comm_rank(comm, &rank);
00210 container->count = container->counts[rank];
00211 container->buffer = (char*)malloc(sizeof(char) * (rank == root ? count : container->count));
00212 return 0;
00213 }
00214
00215 static int finalize(void* _this, MPI_Comm comm, int root) {
00216 MPIB_buffer_container::finalize(_this, comm, root);
00217 MPIB_SGv_container* container = (MPIB_SGv_container*)_this;
00218 free(container->counts);
00219 free(container->displs);
00220 return 0;
00221 }
00222 };
00223
00225 class MPIB_Scatterv_container: public MPIB_SGv_container {
00226 private:
00227 MPIB_Scatterv scatterv;
00228
00229 public:
00230 MPIB_Scatterv_container(MPIB_Scatterv scatterv, const double* factors): MPIB_SGv_container(factors) {
00231 MPIB_coll_container::operation = "Scatterv";
00232 MPIB_coll_container::execute = execute;
00233 this->scatterv = scatterv;
00234 }
00235
00236 static int execute(void* _this, MPI_Comm comm, int root, int M) {
00237 MPIB_Scatterv_container* container = (MPIB_Scatterv_container*)_this;
00238 return container->scatterv(container->buffer, container->counts, container->displs, MPI_CHAR,
00239 container->buffer, container->count, MPI_CHAR,
00240 root, comm);
00241 }
00242 };
00243
00245 class MPIB_Gatherv_container: public MPIB_SGv_container {
00246 private:
00247 MPIB_Gatherv gatherv;
00248
00249 public:
00250 MPIB_Gatherv_container(MPIB_Gatherv gatherv, const double* factors): MPIB_SGv_container(factors) {
00251 MPIB_coll_container::operation = "Gatherv";
00252 MPIB_coll_container::execute = execute;
00253 this->gatherv = gatherv;
00254 }
00255
00256 static int execute(void* _this, MPI_Comm comm, int root, int M) {
00257 MPIB_Gatherv_container* container = (MPIB_Gatherv_container*)_this;
00258 return container->gatherv(container->buffer, container->count, MPI_CHAR,
00259 container->buffer, container->counts, container->displs, MPI_CHAR,
00260 root, comm);
00261 }
00262 };
00263
00265 class MPIB_Alltoall_container: public MPIB_coll_container {
00266 private:
00267 MPIB_Alltoall alltoall;
00268 char* sendbuf;
00269 char* recvbuf;
00270
00271 public:
00272 MPIB_Alltoall_container(MPIB_Alltoall alltoall) {
00273 MPIB_coll_container::operation = "Alltoall";
00274 MPIB_coll_container::initialize = initialize;
00275 MPIB_coll_container::execute = execute;
00276 MPIB_coll_container::finalize = finalize;
00277 this->alltoall = alltoall;
00278 }
00279
00280 static int initialize(void* _this, MPI_Comm comm, int root, int M) {
00281 MPIB_Alltoall_container* container = (MPIB_Alltoall_container*)_this;
00282 int size;
00283 MPI_Comm_size(comm, &size);
00284 container->sendbuf = (char*)malloc(sizeof(char) * M * size);
00285 container->recvbuf = (char*)malloc(sizeof(char) * M * size);
00286 return 0;
00287 }
00288
00289 static int execute(void* _this, MPI_Comm comm, int root, int M) {
00290 MPIB_Alltoall_container* container = (MPIB_Alltoall_container*)_this;
00291 return container->alltoall(container->sendbuf, M, MPI_CHAR,
00292 container->recvbuf, M, MPI_CHAR, comm);
00293 }
00294
00295 static int finalize(void* _this, MPI_Comm comm, int root) {
00296 MPIB_Alltoall_container* container = (MPIB_Alltoall_container*)_this;
00297 free(container->sendbuf);
00298 free(container->recvbuf);
00299 return 0;
00300 }
00301 };
00302
00307 #endif