Fast multidimensional Arrays

Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 29 07:13:41 PDT 2016


Dne 29.8.2016 v 16:08 rikki cattermole via Digitalmars-d-learn napsal(a):

> Okay looks like I've made a boo boo and ldc is compiling out that 
> entire multiplication loop out.
>
> Its passing the array statically and since its never assigned back, 
> its just never compiled in (unless you specify it via ref).
>
> So, this is where I give up as it is 2am.
>
> Perhaps try and make it parallel (std.parallemism can help hugely).
this is my version:

import std.stdio;

immutable int n = 1000, l=1000, m=1000;

struct ZeroDouble
{
     double val = 0f;
     alias val this;
}

void main(string[] args)
{
     auto A = new double [1000][m];
     auto B = new double [1000][n];
     auto C = new ZeroDouble[1000][n];
     ini!(A);
     ini!(B);
     MatMul!(A,B,C);
     writeln(C[1][1]);
     writefln("%d   %d ", C.length, C[0].length);
}

void ini(alias mtx)(){
     foreach(i, ref mtxInner; mtx) {
         foreach(j, ref cell; mtxInner) {
             cell = i*j + 0.6*j +3.4;
         }
     }
}

void MatMul(alias A, alias B, alias C)() {
     foreach(i, ref cv; C) {
         foreach(j, av; A[i]) {
             foreach(k, ref cvv; cv) {
                 cvv += av * B[j][k];
             }
         }
     }
}


More information about the Digitalmars-d-learn mailing list