numeric

C++ library with numerical algorithms
git clone git://src.adamsgaard.dk/numeric
Log | Files | Refs

commit 05d8fdbca877ba617f1813575dfb1a1968c40dc2
parent 2e1958feb0be11837dd7affc6952e6e4a6845d6c
Author: Anders Damsgaard Christensen <adc@geo.au.dk>
Date:   Tue, 22 Jan 2013 22:37:23 +0100

Random number part excluded, codes UNTESTED

Diffstat:
Mmatrixmul/Makefile | 15++++++++++++++-
Mmatrixmul/c-arrofarrs.c | 6+++---
Amatrixmul/c-gsl-cblas.c | 37+++++++++++++++++++++++++++++++++++++
Mmatrixmul/c-linarr.c | 6+++---
Mmatrixmul/cpp-linvectors.cpp | 8++++----
Mmatrixmul/cpp-vectorofvectors.cpp | 6+++---
Mmatrixmul/julia.jl | 6+++---
Mmatrixmul/lua-arrofarrs.lua | 2+-
Mmatrixmul/lua-linarr.lua | 2+-
Mmatrixmul/octave.m | 2+-
Mmatrixmul/plot.gp | 17+++++++++++++++--
Mmatrixmul/python-numpy.py | 2+-
12 files changed, 86 insertions(+), 23 deletions(-)

diff --git a/matrixmul/Makefile b/matrixmul/Makefile @@ -17,7 +17,7 @@ CXX=g++ CFLAGS=-Wall -O3 CXXFLAGS=-Wall -O3 -performance.png: plot.gp lua-arrofarrs.dat lua-linarr.dat luajit-arrofarrs.dat luajit-linarr.dat c-arrofarrs.dat c-linarr.dat c-omp-arrofarrs.dat c-omp-linarr.dat julia.dat cpp-vectorofvectors.dat cpp-linvectors.dat python-numpy.dat octave.dat +performance.png: plot.gp lua-arrofarrs.dat lua-linarr.dat luajit-arrofarrs.dat luajit-linarr.dat c-arrofarrs.dat c-linarr.dat c-omp-arrofarrs.dat c-omp-linarr.dat c-gsl-cblas.dat julia.dat cpp-vectorofvectors.dat cpp-linvectors.dat python-numpy.dat octave.dat gnuplot plot.gp # Lua: Matrices as arrays of arrays @@ -97,6 +97,18 @@ c-omp-arrofarrs: c-arrofarrs.c c-omp-linarr: c-linarr.c $(CC) $(CFLAGS) -fopenmp $< -o $@ + +# C: GSL CBLAS library +c-gsl-cblas.dat: c-gsl-cblas + # c-gsl-cblas + @rm -f $@ + @for dims in $(MATRIXDIMS); do \ + $(PREFIXCMD) $@ -f "$$dims %e" ./$< $$dims; \ + echo $$dims; \ + done + +c-gsl-cblas: c-gsl-cblas.c + $(CC) $(CFLAGS) -lgslcblas $< -o $@ # Julia, native arrays julia.dat: julia.jl @@ -149,6 +161,7 @@ clean: $(RM) *.png $(RM) c-arrofarrs c-linarr $(RM) c-omp-arrofarrs c-omp-linarr + $(RM) c-gsl-cblas $(RM) cpp-vectorofvectors cpp-linvectors edit: diff --git a/matrixmul/c-arrofarrs.c b/matrixmul/c-arrofarrs.c @@ -1,5 +1,5 @@ -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> void matrixMult(double** A, double** B, double** C, int N) { @@ -20,7 +20,7 @@ void matrixMult(double** A, double** B, double** C, int N) int main(int argc, char* argv[]) { - int i, j, N; + unsigned int i, j, N; double** A; double** B; double** C; @@ -45,7 +45,7 @@ int main(int argc, char* argv[]) for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { A[i][j] = 2.0; - B[i][j] = rand()/RAND_MAX; + B[i][j] = (double) N*j + i; } } diff --git a/matrixmul/c-gsl-cblas.c b/matrixmul/c-gsl-cblas.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stdlib.h> +#include <gsl/gsl_cblas.h> + +int main(int argc, char* argv[]) +{ + unsigned int i, N; + double* A; + double* B; + double* C; + + if (argc == 2) { + N = atoi(argv[1]); + } else { + printf("Sorry, I need matrix width as command line argument\n"); + return 1; + } + + A = (double*) malloc(N * N * sizeof(double*)); + B = (double*) malloc(N * N * sizeof(double*)); + C = (double*) malloc(N * N * sizeof(double*)); + + for (i = 0; i < N*N; i++) { + A[i] = 2.0; + B[i] = (double)i; + } + + cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, + N, N, N, 1.0, A, N, B, N, 0.0, C, N); + + free(A); + free(B); + free(C); + + /* Exit with success */ + return 0; +} diff --git a/matrixmul/c-linarr.c b/matrixmul/c-linarr.c @@ -1,5 +1,5 @@ -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> void matrixMult(double* A, double* B, double* C, int N) { @@ -20,7 +20,7 @@ void matrixMult(double* A, double* B, double* C, int N) int main(int argc, char* argv[]) { - int i, N; + unsigned int i, N; double* A; double* B; double* C; @@ -38,7 +38,7 @@ int main(int argc, char* argv[]) for (i = 0; i < N*N; i++) { A[i] = 2.0; - B[i] = rand()/RAND_MAX; + B[i] = (double)i; } matrixMult(A, B, C, N); diff --git a/matrixmul/cpp-linvectors.cpp b/matrixmul/cpp-linvectors.cpp @@ -1,13 +1,13 @@ #include <iostream> -#include <vector> #include <cstdlib> +#include <vector> int main(int argc, char* argv[]) { using std::cout; using std::vector; - int N, i, j, k; + unsigned int N, i, j, k; if (argc == 2) { N = atoi(argv[1]); @@ -23,14 +23,14 @@ int main(int argc, char* argv[]) for (i = 0; i<N; ++i) { for (j = 0; j<N; ++j) { A[j*N+i] = 2.0; - B[j*N+i] = rand()/RAND_MAX; + B[j*N+i] = (double)i; } } double sum; for (i = 0; i < N; ++i) { for (j = 0; j < N; ++j) { - sum = 0.0f; + sum = 0.0; for (k = 0; k < N; ++k) sum += A[k*N+i] * B[j*N+k]; C[j*N+i] = sum; diff --git a/matrixmul/cpp-vectorofvectors.cpp b/matrixmul/cpp-vectorofvectors.cpp @@ -1,13 +1,13 @@ #include <iostream> -#include <vector> #include <cstdlib> +#include <vector> int main(int argc, char* argv[]) { using std::cout; using std::vector; - int N, i, j, k; + unsigned int N, i, j, k; if (argc == 2) { N = atoi(argv[1]); @@ -23,7 +23,7 @@ int main(int argc, char* argv[]) for (i = 0; i<N; ++i) { for (j = 0; j<N; ++j) { A[i][j] = 2.0; - B[i][j] = rand()/RAND_MAX; + B[i][j] = (double) N*j + i; } } diff --git a/matrixmul/julia.jl b/matrixmul/julia.jl @@ -1,11 +1,11 @@ #!/usr/bin/env julia if (length(ARGS) == 1) - N = int(ARGS[1]) + const N = int(ARGS[1]) else println("Sorry, I need the matrix width as a command line argument\n") end -A = ones(N,N)*2.0 -B = rand(N,N) +const A = ones(N, N)*2.0 +const B = reshape(0.0:N*N-1, N, N) C = A*B diff --git a/matrixmul/lua-arrofarrs.lua b/matrixmul/lua-arrofarrs.lua @@ -12,7 +12,7 @@ for i=1,N do C[i] = {} for j=1,N do A[i][j] = 2 - B[i][j] = math.random() + B[i][j] = (N * j-1) + i-1 end end diff --git a/matrixmul/lua-linarr.lua b/matrixmul/lua-linarr.lua @@ -7,7 +7,7 @@ A = {} B = {} for i=1,(N*N) do A[i] = 2.0 - B[i] = math.random() + B[i] = i-1 end C = {} diff --git a/matrixmul/octave.m b/matrixmul/octave.m @@ -8,5 +8,5 @@ else end A = ones(N,N) * 2.0; -B = rand(N,N); +B = reshape(0:N*N-1, N, N); C = A*B; diff --git a/matrixmul/plot.gp b/matrixmul/plot.gp @@ -2,8 +2,21 @@ set terminal png size 1200,600 set output "performance.png" set xlabel "Matrix side length" set ylabel "Execution time [s]" -set title "Random number generation and matrix multiplication" +set title "Matrix multiplication" #set log xy set grid set key outside -plot "lua-arrofarrs.dat" title "Lua: Arrays of arrays" w lp, "lua-linarr.dat" title "Lua: Linear arrays" w lp, "luajit-arrofarrs.dat" title "LuaJIT: Arrays of arrays" w lp, "luajit-linarr.dat" title "LuaJIT: Linear arrays" w lp, "c-arrofarrs.dat" title "C: Arrays of arrays" w lp, "c-linarr.dat" title "C: Linear arrays" w lp, "c-omp-arrofarrs.dat" title "C-OpenMP: Arrays of arrays" w lp, "c-omp-linarr.dat" title "C-OpenMP: Linear arrays" w lp, "julia.dat" title "Julia" w lp, "cpp-vectorofvectors.dat" title "C++: Vectors of vectors" w lp, "cpp-linvectors.dat" title "C++: Linear vectors" w lp, "python-numpy.dat" title "Python: Numpy" w lp, "octave.dat" title "Octave" w lp +plot "lua-arrofarrs.dat" title "Lua: Arrays of arrays" w lp, \ + "lua-linarr.dat" title "Lua: Linear arrays" w lp, \ + "luajit-arrofarrs.dat" title "LuaJIT: Arrays of arrays" w lp, \ + "luajit-linarr.dat" title "LuaJIT: Linear arrays" w lp, \ + "c-arrofarrs.dat" title "C: Arrays of arrays" w lp, \ + "c-linarr.dat" title "C: Linear arrays" w lp, \ + "c-omp-arrofarrs.dat" title "C-OpenMP: Arrays of arrays" w lp, \ + "c-omp-linarr.dat" title "C-OpenMP: Linear arrays" w lp, \ + "c-gsl-cblas.dat" title "C: GSL CBLAS" w lp, \ + "julia.dat" title "Julia" w lp, \ + "cpp-vectorofvectors.dat" title "C++: Vectors of vectors" w lp, \ + "cpp-linvectors.dat" title "C++: Linear vectors" w lp, \ + "python-numpy.dat" title "Python: Numpy" w lp, \ + "octave.dat" title "Octave" w lp diff --git a/matrixmul/python-numpy.py b/matrixmul/python-numpy.py @@ -9,5 +9,5 @@ else : sys.exit(1) A = numpy.ones((N,N))*2.0 -B = numpy.random.random_sample((N,N)) +B = numpy.arange(0,N*N).reshape(N,N) C = numpy.dot(A,B)