openmp-parallel-matmul.ino
C program using OpenMP to accelerate matrix multiplication on the V40's 4 cores.
Compile with gcc -fopenmp -O2 matmul.c -o matmul. The Allwinner V40 has 4 Cortex-A7 cores. OpenMP distributes tasks across these cores. Compare the time with and without the #pragma omp parallel for directive to measure the gain.
openmp-parallel-matmul.ino
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define N 1000
int main() {
int i, j, k;
double start_time, run_time;
double *A, *B, *C;
// Allocation mémoire
A = (double*)malloc(N*N * sizeof(double));
B = (double*)malloc(N*N * sizeof(double));
C = (double*)malloc(N*N * sizeof(double));
// Initialisation
for(i=0; i<N*N; i++) {
A[i] = 1.0;
B[i] = 2.0;
C[i] = 0.0;
}
start_time = omp_get_wtime();
// Multiplication de matrices parallélisée
#pragma omp parallel for shared(A,B,C) private(i,j,k) schedule(static)
for(i=0; i<N; i++) {
for(j=0; j<N; j++) {
for(k=0; k<N; k++) {
C[i*N + j] += A[i*N + k] * B[k*N + j];
}
}
}
run_time = omp_get_wtime() - start_time;
printf("Temps d'exécution: %f secondes\n", run_time);
printf("Résultat C[0][0]: %f\n", C[0]);
free(A); free(B); free(C);
return 0;
}