Commit 919f664a authored by Alexandru Dura's avatar Alexandru Dura

Add a simple code to compute the norm

parent 797e2317
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <assert.h>
#include <stdint.h>
#include <float.h>
#include "common.h"
#ifdef _OPENMP
#include <omp.h>
#endif
#define STR(s) #s
#define XSTR(s) STR(s)
#define SCHEDULE static
#define N_RUNS 100
#define VEC_SIZE 50000000
#define NOINLINE __attribute__((noinline))
void NOINLINE initialize(double *d, int n, double val) {
#pragma omp parallel for \
schedule(SCHEDULE)
for (int i = 0; i < n; ++i) {
d[i] = val;
}
}
double NOINLINE sqsum(const double *d, int n) {
double sum = 0.0;
#pragma omp parallel for \
reduction(+:sum) \
schedule(SCHEDULE)
for (int i = 0; i < n; ++i) {
sum += d[i] * d[i];
}
return sum;
}
double NOINLINE norm_experiment(double *d, int n) {
initialize(d, n, 1.0);
double s = sqsum(d, n);
return sqrt(s);
}
int main(int argc, char **argv) {
double r;
double min_time = DBL_MAX;
double *d = (double*) malloc(VEC_SIZE * sizeof(double));
for (int i = 0; i < N_RUNS; ++i) {
double elapsed_time;
TIME_REGION(elapsed_time,
r = norm_experiment(d, VEC_SIZE));
min_time = MIN(min_time, elapsed_time);
}
int n_threads;
#ifdef _OPENMP
#pragma omp parallel
{
n_threads = omp_get_num_threads();
}
#else
n_threads = 1;
#endif
printf ("%d," XSTR(SCHEDULE) ",%f,%f\n", n_threads, min_time, r);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment