-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_kernels.c
63 lines (55 loc) · 1.97 KB
/
vector_kernels.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*******************************************************************************************/
/* This file is part of the training material available at */
/* https://github.com/jthies/PELS */
/* You may redistribute it and/or modify it under the terms of the BSD-style licence */
/* included in this software. */
/* */
/* Contact: Jonas Thies ([email protected]) */
/* */
/*******************************************************************************************/
#include <sys/types.h>
#include <stdio.h>
#include <math.h>
// Basic C implemenattions of axpy and dot using OpenMP.
// copy y = x with multiple threads
void copy_vector(size_t N, double *restrict X, double *restrict Y)
{
#pragma omp parallel for schedule(static)
for (size_t i = 0; i < N; i++)
{
Y[i]=X[i];
}
}
// Operation that loads two and writes one vector (z=a*x+y).
void axpby(size_t N, double a, const double *restrict X, double b, double *restrict Y)
{
#pragma omp parallel for schedule(static)
for (size_t i = 0; i < N; i++)
{
Y[i]=a*X[i]+b*Y[i];
}
}
void init(size_t N, double *restrict X, double val)
{
#pragma omp parallel for schedule(static)
for (size_t i = 0; i < N; i++)
{
X[i]=val;
}
}
// vector scaling y[i] = v[i]*x[i]
void vscale(size_t N, double const *restrict V, double const *restrict X, double *restrict Y)
{
#pragma omp parallel for schedule(static)
for (size_t i = 0; i < N; i++)
{
Y[i]=V[i]*X[i];
}
}
double dot(size_t N, const double* X, const double* Y)
{
double dot=0.0;
#pragma omp parallel for schedule(static) reduction(+:dot)
for (size_t i=0; i<N; i++) dot+=X[i]*Y[i];
return dot;
}