tRandom number part excluded, codes UNTESTED - numeric - C++ library with numerical algorithms
(HTM) git clone git://src.adamsgaard.dk/numeric
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
(DIR) commit 05d8fdbca877ba617f1813575dfb1a1968c40dc2
(DIR) parent 2e1958feb0be11837dd7affc6952e6e4a6845d6c
(HTM) Author: Anders Damsgaard Christensen <adc@geo.au.dk>
Date: Tue, 22 Jan 2013 22:37:23 +0100
Random number part excluded, codes UNTESTED
Diffstat:
M matrixmul/Makefile | 15 ++++++++++++++-
M matrixmul/c-arrofarrs.c | 6 +++---
A matrixmul/c-gsl-cblas.c | 37 +++++++++++++++++++++++++++++++
M matrixmul/c-linarr.c | 6 +++---
M matrixmul/cpp-linvectors.cpp | 8 ++++----
M matrixmul/cpp-vectorofvectors.cpp | 6 +++---
M matrixmul/julia.jl | 6 +++---
M matrixmul/lua-arrofarrs.lua | 2 +-
M matrixmul/lua-linarr.lua | 2 +-
M matrixmul/octave.m | 2 +-
M matrixmul/plot.gp | 17 +++++++++++++++--
M matrixmul/python-numpy.py | 2 +-
12 files changed, 86 insertions(+), 23 deletions(-)
---
(DIR) diff --git a/matrixmul/Makefile b/matrixmul/Makefile
t@@ -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
t@@ -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
t@@ -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:
(DIR) diff --git a/matrixmul/c-arrofarrs.c b/matrixmul/c-arrofarrs.c
t@@ -1,5 +1,5 @@
-#include <stdlib.h>
#include <stdio.h>
+#include <stdlib.h>
void matrixMult(double** A, double** B, double** C, int N)
{
t@@ -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;
t@@ -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;
}
}
(DIR) diff --git a/matrixmul/c-gsl-cblas.c b/matrixmul/c-gsl-cblas.c
t@@ -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;
+}
(DIR) diff --git a/matrixmul/c-linarr.c b/matrixmul/c-linarr.c
t@@ -1,5 +1,5 @@
-#include <stdlib.h>
#include <stdio.h>
+#include <stdlib.h>
void matrixMult(double* A, double* B, double* C, int N)
{
t@@ -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;
t@@ -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);
(DIR) diff --git a/matrixmul/cpp-linvectors.cpp b/matrixmul/cpp-linvectors.cpp
t@@ -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]);
t@@ -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;
(DIR) diff --git a/matrixmul/cpp-vectorofvectors.cpp b/matrixmul/cpp-vectorofvectors.cpp
t@@ -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]);
t@@ -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;
}
}
(DIR) diff --git a/matrixmul/julia.jl b/matrixmul/julia.jl
t@@ -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
(DIR) diff --git a/matrixmul/lua-arrofarrs.lua b/matrixmul/lua-arrofarrs.lua
t@@ -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
(DIR) diff --git a/matrixmul/lua-linarr.lua b/matrixmul/lua-linarr.lua
t@@ -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 = {}
(DIR) diff --git a/matrixmul/octave.m b/matrixmul/octave.m
t@@ -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;
(DIR) diff --git a/matrixmul/plot.gp b/matrixmul/plot.gp
t@@ -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
(DIR) diff --git a/matrixmul/python-numpy.py b/matrixmul/python-numpy.py
t@@ -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)