Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use an intermediate variable to compute the sum and access c[i][j] only once.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 10 Apr 2023 20:03:39 +0000 (22:03 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 10 Apr 2023 20:18:59 +0000 (22:18 +0200)
May fix crash when built with Intel(R) oneAPI DPC++/C++ Compiler 2023.1.0 (2023.1.0.20230320)
Probably an optimization bug!

Also clean up the function signature.

examples/smpi/gemm/gemm.c

index 3aedd09..b9f0635 100644 (file)
 #include <mpi.h>
 #include <stdio.h>
 
-void multiply(float* a, float* b, float* c, int istart, int iend, int size);
-void multiply_sampled(float* a, float* b, float* c, int istart, int iend, int size);
-
-
-void multiply(float* a, float* b, float* c, int istart, int iend, int size)
+static void multiply(const float* a, const float* b, float* c, int istart, int iend, int size)
 {
     for (int i = istart; i <= iend; ++i) {
         for (int j = 0; j < size; ++j) {
-            for (int k = 0; k < size; ++k) {
-                c[i*size+j] += a[i*size+k] * b[k*size+j];
-            }
+          float sum = 0.0;
+          for (int k = 0; k < size; ++k) {
+            sum += a[i * size + k] * b[k * size + j];
+          }
+          c[i * size + j] += sum;
         }
     }
 }
 
-void multiply_sampled(float* a, float* b, float* c, int istart, int iend, int size)
+static void multiply_sampled(const float* a, const float* b, float* c, int istart, int iend, int size)
 {
     //for (int i = istart; i <= iend; ++i) {
     SMPI_SAMPLE_GLOBAL (int i = istart, i <= iend, ++i, 10, 0.005){
         for (int j = 0; j < size; ++j) {
-            for (int k = 0; k < size; ++k) {
-                c[i*size+j] += a[i*size+k] * b[k*size+j];
-            }
+          float sum = 0.0;
+          for (int k = 0; k < size; ++k) {
+            sum += a[i * size + k] * b[k * size + j];
+          }
+          c[i * size + j] += sum;
         }
     }
 }