Skip to content

Commit

Permalink
switch to native complex numbers (#367)
Browse files Browse the repository at this point in the history
* rework glas

* add const

* use native complex numbers

* remove complex and update sum

* fix style
  • Loading branch information
9il authored Oct 17, 2016
1 parent 4e82864 commit e8b5f08
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 332 deletions.
29 changes: 13 additions & 16 deletions benchmarks/glas/gemm_bench.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
/+ dub.json:
{
"name": "gemm_bench",
"dependencies": {"mir": {"path": "../.."}, "cblas": "~>0.1.0"},
"dependencies": {"mir": {"path": "../.."}, "cblas": "~>1.0.0"},
"dflags-ldc": ["-mcpu=native"],
"libs": ["blas"],
"lflags": ["-L./"]
}
+/
import std.math;
import std.traits;
import std.datetime;
import std.conv;
import std.complex;
import std.algorithm.comparison;
import std.stdio;
import std.exception;
import std.getopt;
import mir.ndslice;
import mir.glas;
import mir.internal.utility : isComplex;

alias C = float;
//alias C = double;
//alias C = Complex!float;
//alias C = Complex!double;
//alias C = cfloat;
//alias C = cdouble;
alias A = C;
alias B = C;

void main(string[] args)
{
auto glas = new GlasContext;
size_t m = 1000;
size_t n = size_t.max;
size_t k = size_t.max;
Expand Down Expand Up @@ -60,28 +60,26 @@ void main(string[] args)

d[] = c[];

static if(is(C : Complex!F, F))
static if(isComplex!C)
{
C alpha = C(3, 7);
C beta = C(2, 5);
C alpha = 3 + 7i;
C beta = 2 + 5i;
}
else
{
C alpha = 3;
C beta = 2;
}


auto nsecsBLAS = double.max;


foreach(_; 0..count) {
StopWatch sw;
sw.start;
static if(!(is(C == real) || is(C : Complex!real) || is(C : long)))
static if(!(is(C == real) || is(C == creal) || is(C : long)))
{
static import cblas;
static if(is(C : Complex!E, E))
static if(isComplex!C)
cblas.gemm(
cblas.Order.RowMajor,
cblas.Transpose.NoTrans,
Expand Down Expand Up @@ -128,7 +126,7 @@ void main(string[] args)
{
StopWatch sw;
sw.start;
glas.gemm(alpha, a, b, beta, c);
gemm(alpha, a, b, beta, c);
sw.stop;
auto newns = sw.peek.to!Duration.total!"nsecs".to!double;
//writefln("_GLAS (single thread) : %5s GFLOPS", (m * n * k * 2) / newns);
Expand All @@ -147,10 +145,9 @@ void fillRNG(T)(Slice!(2, T*) sl)
import std.random;
foreach(ref e; sl.byElement)
{
static if(is(T : Complex!F, F))
static if(isComplex!T)
{
e.re = cast(F) uniform(-100, 100);
e.im = cast(F) uniform(-100, 100);
e = uniform(-100, 100) + uniform(-100, 100) * 1i;
}
else
{
Expand Down
28 changes: 13 additions & 15 deletions benchmarks/glas/gemm_report.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
/+ dub.json:
{
"name": "gemm_report",
"dependencies": {"mir": {"path": "../.."}, "cblas": "~>0.2.0"},
"dependencies": {"mir": {"path": "../.."}, "cblas": "~>1.0.0"},
"lflags": ["-L./"],
"libs": ["blas"],
"dflags-ldc": ["-mcpu=native", "-singleobj"],
"dflags-ldc": ["-mcpu=native"],
}
+/
//"lflags": ["-L/opt/intel/mkl/lib"],
Expand All @@ -19,18 +19,18 @@ import std.math;
import std.traits;
import std.datetime;
import std.conv;
import std.complex;
import std.algorithm.comparison;
import std.stdio;
import std.exception;
import std.getopt;
import mir.ndslice;
import mir.glas;
import mir.internal.utility : isComplex;

alias C = float;
//alias C = double;
//alias C = Complex!float;
//alias C = Complex!double;
//alias C = cfloat;
//alias C = cdouble;
alias A = C;
alias B = C;

Expand All @@ -41,7 +41,6 @@ size_t[] reportValues = [

void main(string[] args)
{
auto glas = new GlasContext;
size_t count = 6;
auto helpInformation =
getopt(args,
Expand Down Expand Up @@ -69,10 +68,10 @@ void main(string[] args)

d[] = c[];

static if(is(C : Complex!F, F))
static if(isComplex!C)
{
C alpha = C(3, 7);
C beta = C(2, 5);
C alpha = 3 + 7i;
C beta = 2 + 5i;
}
else
{
Expand All @@ -85,10 +84,10 @@ void main(string[] args)
foreach(_; 0..count) {
StopWatch sw;
sw.start;
static if(!(is(C == real) || is(C : Complex!real) || is(C : long)))
static if(!(is(C == real) || is(C == creal) || is(C : long)))
{
static import cblas;
static if(is(C : Complex!E, E))
static if(isComplex!C)
cblas.gemm(
cblas.Order.RowMajor,
cblas.Transpose.NoTrans,
Expand Down Expand Up @@ -137,7 +136,7 @@ void main(string[] args)
{
StopWatch sw;
sw.start;
glas.gemm(alpha, a, b, beta, c);
gemm(alpha, a, b, beta, c);
sw.stop;
auto newns = sw.peek.to!Duration.total!"nsecs".to!double;
//writefln("_GLAS (single thread) : %5s GFLOPS", (m * n * k * 2) / newns);
Expand All @@ -154,10 +153,9 @@ void fillRNG(T)(Slice!(2, T*) sl)
import std.random;
foreach(ref e; sl.byElement)
{
static if(is(T : Complex!F, F))
static if(isComplex!T)
{
e.re = cast(F) uniform(-100, 100);
e.im = cast(F) uniform(-100, 100);
e = uniform(-100, 100) + uniform(-100, 100) * 1i;
}
else
{
Expand Down
29 changes: 13 additions & 16 deletions benchmarks/glas/symm_bench.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
/+ dub.json:
{
"name": "symm_bench",
"dependencies": {"mir": {"path": "../.."}, "cblas": "~>0.1.0"},
"dependencies": {"mir": {"path": "../.."}, "cblas": "~>1.0.0"},
"dflags-ldc": ["-mcpu=native"],
"libs": ["blas"],
"lflags": ["-L./"]
}
+/
import std.math;
import std.traits;
import std.datetime;
import std.conv;
import std.complex;
import std.algorithm.comparison;
import std.stdio;
import std.exception;
import std.getopt;
import mir.ndslice;
import mir.glas;
import mir.internal.utility : isComplex;

alias C = float;
//alias C = double;
//alias C = Complex!float;
//alias C = Complex!double;
//alias C = cfloat;
//alias C = cdouble;
alias A = C;
alias B = C;

void main(string[] args)
{
auto glas = new GlasContext;
size_t m = 1000;
size_t n = size_t.max;
size_t k = size_t.max;
Expand Down Expand Up @@ -59,28 +59,26 @@ void main(string[] args)

d[] = c[];

static if(is(C : Complex!F, F))
static if(isComplex!C)
{
C alpha = C(3, 7);
C beta = C(2, 5);
C alpha = 3 + 7i;
C beta = 2 + 5i;
}
else
{
C alpha = 3;
C beta = 2;
}


auto nsecsBLAS = double.max;


foreach(_; 0..count) {
StopWatch sw;
sw.start;
static if(!(is(C == real) || is(C : Complex!real) || is(C : long)))
static if(!(is(C == real) || is(C == creal) || is(C : long)))
{
static import cblas;
static if(is(C : Complex!E, E))
static if(isComplex!C)
cblas.symm(
cblas.Order.RowMajor,
cblas.Side.Left,
Expand Down Expand Up @@ -125,7 +123,7 @@ void main(string[] args)
{
StopWatch sw;
sw.start;
glas.symm(Side.left, Uplo.lower, alpha, a, b, beta, c);
symm(Side.left, Uplo.lower, alpha, a, b, beta, c);
sw.stop;
auto newns = sw.peek.to!Duration.total!"nsecs".to!double;
//writefln("_GLAS (single thread) : %5s GFLOPS", (m * n * m * 2) / newns);
Expand All @@ -148,10 +146,9 @@ void fillRNG(T)(Slice!(2, T*) sl)
import std.random;
foreach(ref e; sl.byElement)
{
static if(is(T : Complex!F, F))
static if(isComplex!T)
{
e.re = cast(F) uniform(-100, 100);
e.im = cast(F) uniform(-100, 100);
e = uniform(-100, 100) + uniform(-100, 100) * 1i;
}
else
{
Expand Down
52 changes: 1 addition & 51 deletions source/mir/glas/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,6 @@ Authors: Ilya Yaroshenko
+/
module mir.glas.common;

/++
GLAS Context
Note: `GlasContext` is single thread for now.
+/
struct GlasContext
{
import mir.internal.memory;
import mir.glas.internal.context;
static import cpuid.unified;
import core.sync.mutex;

private
{
void[] _memory;
}

nothrow @nogc:

~this()
{
release;
}

/// Returns: reused unaligned memory chunk
nothrow @nogc void[] memory(size_t size)
{
if (_memory.length < size)
{
auto f = _memory.length << 1;
if (f > size)
size = f;
if (_memory !is null)
deallocate(_memory);
_memory = alignedAllocate(size, 4096);
}
return _memory[0 .. size];
}

/// Releases memory.
void release()
{
if (_memory !is null)
deallocate(_memory);
}
}

/++
Uplo specifies whether a matrix is an upper or lower triangular matrix.
+/
Expand Down Expand Up @@ -153,10 +106,7 @@ package mixin template prefix3()
enum PB = CB ? 2 : 1;
enum PC = CC ? 2 : 1;

static if (is(C : Complex!F, F))
alias T = F;
else
alias T = C;
alias T = realType!C;
static assert(!isComplex!T);
}

Expand Down
Loading

0 comments on commit e8b5f08

Please sign in to comment.