1 /*==================================================================================================*/
2 /*# This file is part of the CodeVault project. The project is licensed under Apache Version 2.0.*/
3 /*# CodeVault is part of the EU-project PRACE-4IP (WP7.3.C).*/
6 /*# Valeriu Codreanu <valeriu.codreanu@surfsara.nl>*/
8 /*# ==================================================================================================*/
13 void multiply(float* a, float* b, float* c, int istart, int iend, int size);
14 void multiply_sampled(float* a, float* b, float* c, int istart, int iend, int size);
17 void multiply(float* a, float* b, float* c, int istart, int iend, int size)
19 for (int i = istart; i <= iend; ++i) {
20 for (int j = 0; j < size; ++j) {
21 for (int k = 0; k < size; ++k) {
22 c[i*size+j] += a[i*size+k] * b[k*size+j];
28 void multiply_sampled(float* a, float* b, float* c, int istart, int iend, int size)
30 //for (int i = istart; i <= iend; ++i) {
31 SMPI_SAMPLE_GLOBAL (int i = istart, i <= iend, ++i, 10, 0.005){
32 for (int j = 0; j < size; ++j) {
33 for (int k = 0; k < size; ++k) {
34 c[i*size+j] += a[i*size+k] * b[k*size+j];
40 int main(int argc, char* argv[])
46 MPI_Init(&argc, &argv);
47 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
48 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
52 printf("Usage : gemm size \"native/sampling\"\n");
57 int read = sscanf(argv[1], "%d", &size);
60 printf("Invalid argument %s\n", argv[1]);
64 printf("Matrix Size : %dx%d\n",size,size);
67 float *a = (float*)malloc(sizeof(float)*size*size);
68 float *b = (float*)malloc(sizeof(float)*size*size);
69 float *c = (float*)malloc(sizeof(float)*size*size);
72 MPI_Barrier(MPI_COMM_WORLD);
76 // Initialize buffers.
77 for (int i = 0; i < size; ++i) {
78 for (int j = 0; j < size; ++j) {
79 a[i*size+j] = (float)i + j;
80 b[i*size+j] = (float)i - j;
86 // Broadcast matrices to all workers.
87 MPI_Bcast(a, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
88 MPI_Bcast(b, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
89 MPI_Bcast(c, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
91 // Partition work by i-for-loop.
92 istart = (size / nproc) * rank;
93 iend = (size / nproc) * (rank + 1) - 1;
95 // Compute matrix multiplication in [istart,iend]
98 if (strcmp(argv[2], "sampling")){
100 printf ("Native mode\n");
101 multiply(a, b, c, istart, iend, size);
104 printf ("Sampling mode\n");
105 multiply_sampled(a, b, c, istart, iend, size);
108 // Gather computed results.
109 MPI_Gather(c + (size/nproc*rank),
112 c + (size/nproc*rank),
119 // Compute remaining multiplications
120 // when size % nproc > 0.
121 if (size % nproc > 0) {
122 if (strcmp(argv[2], "sampling"))
123 multiply(a, b, c, (size/nproc)*nproc, size-1, size);
125 multiply_sampled(a, b, c, (size/nproc)*nproc, size-1, size);
129 MPI_Barrier(MPI_COMM_WORLD);
136 if (rank == 0) { /* use time on master node */
137 //float msec_total = 0.0f;
139 // Compute and print the performance
140 float sec_per_matrix_mul = end-start;
141 double flops_per_matrix_mul = 2.0 * (double)size * (double)size * (double)size;
142 double giga_flops = (flops_per_matrix_mul * 1.0e-9f) / (sec_per_matrix_mul / 1000.0f);
144 "Performance= %.2f GFlop/s, Time= %.3f sec, Size= %.0f Ops\n",
147 flops_per_matrix_mul);