First Commit
This commit is contained in:
57
openmp_add.c
Normal file
57
openmp_add.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
#include <omp.h>
|
||||
|
||||
#define ARRAY_SIZE 15000576 // 100 million items
|
||||
|
||||
double get_time() {
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
return tv.tv_sec + tv.tv_usec * 1e-6;
|
||||
}
|
||||
|
||||
int main() {
|
||||
float *a = (float*)malloc(sizeof(float) * ARRAY_SIZE);
|
||||
float *b = (float*)malloc(sizeof(float) * ARRAY_SIZE);
|
||||
float *c = (float*)malloc(sizeof(float) * ARRAY_SIZE);
|
||||
|
||||
// Initialize arrays (single-threaded)
|
||||
double start = get_time();
|
||||
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||
a[i] = i;
|
||||
b[i] = i * 2;
|
||||
}
|
||||
double init_time = get_time() - start;
|
||||
|
||||
// Set number of threads
|
||||
omp_set_num_threads(8);
|
||||
|
||||
// Perform the calculation (parallelized with OpenMP)
|
||||
start = get_time();
|
||||
// Try different schedules:
|
||||
// #pragma omp parallel for schedule(static) // Let the compiler choose chunk size
|
||||
// #pragma omp parallel for schedule(dynamic, 256) // Dynamic scheduling
|
||||
#pragma omp parallel for schedule(static, 256) // Smaller chunk size
|
||||
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||
c[i] = a[i] + b[i];
|
||||
}
|
||||
double calc_time = get_time() - start;
|
||||
|
||||
// Print timing results
|
||||
printf("Array initialization time: %.3f ms\n", init_time * 1000);
|
||||
printf("Calculation time: %.3f ms\n", calc_time * 1000);
|
||||
printf("Total time: %.3f ms\n", (init_time + calc_time) * 1000);
|
||||
|
||||
// Print a sample of the result
|
||||
for (int i = 0; i < 10; i++) {
|
||||
printf("c[%d] = %f\n", i, c[i]);
|
||||
}
|
||||
|
||||
// Clean up
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user