Skip to content

Instantly share code, notes, and snippets.

@cipri-tom
Last active August 29, 2015 14:11
Show Gist options
  • Save cipri-tom/ff3952057e88f2a46ac6 to your computer and use it in GitHub Desktop.
Save cipri-tom/ff3952057e88f2a46ac6 to your computer and use it in GitHub Desktop.
LuaJIT and C OpenMP trials
# make with different optimisation levels:
# make O_LEVEL=2 will make with -O2
CC=gcc
O_LEVEL=0
OMP=-fopenmp
CFLAGS=-std=c99 -Wall -pedantic $(OMP) -O$(O_LEVEL)
NL=10
SZ=64
test-$(O_LEVEL): test.c
$(CC) $(CFLAGS) -shared -fPIC test.c -o libtest.so
$(CC) $(CFLAGS) -DMAIN test.c -o test
time ./test $(NL) $(SZ)
time luajit test.lua $(NL) $(SZ)
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <time.h>
// for running from lua:
// gcc -std=c99 -O3 -Wall -pedantic -fopenmp -shared -fPIC test.c -o libtest.so
// for running from C:
// gcc -std=c99 -O3 -Wall -pedantic -fopenmp -DMAIN test.c -o test
#ifdef _OPENMP
#include <omp.h>
#else
static int
omp_get_num_procs(void)
{
return 1;
}
#endif
static double
dot_prod(int n, double a[n], double b[n])
{
double sum = 0;
#ifdef _OPENMP
#pragma omp parallel for reduction(+:sum)
#endif
for (int i = 0; i < n; ++i)
sum += a[i] * b[i];
return sum;
}
void
do_parallel(int NL, int SZ)
{
double *a = malloc(SZ * sizeof(double));
double *b = malloc(SZ * sizeof(double));
double sum = 0;
assert(a && b);
for (int i = 0; i < SZ; ++i)
a[i] = b[i] = sqrt(i+1);
double t0 = clock();
for (int l = 0; l < NL; ++l)
sum += dot_prod(SZ, a, b);
double dt = (clock()-t0)/CLOCKS_PER_SEC;
free(a);
free(b);
printf("sum = %g, time = %.3f, threads = %d\n", sum, dt/omp_get_num_procs(), omp_get_num_procs());
}
#ifdef MAIN
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int NL = 10;
int SZ = 64;
if (argc >= 2) NL = atoi(argv[1]);
if (argc >= 3) SZ = atoi(argv[2]);
for (int l = 0; l < 10; ++l)
do_parallel(NL, SZ);
exit(EXIT_SUCCESS);
}
#endif
local ffi = require "ffi"
local testLib = ffi.load("./libtest.so")
ffi.cdef[[
void do_parallel(int,int);
]]
local NL = tonumber(arg[1]) or 10
local SZ = tonumber(arg[2]) or 64
for l=1,10 do
testLib.do_parallel(NL, SZ)
end
os.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment