Fast multidimensional Arrays

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 29 06:57:06 PDT 2016


On 30/08/2016 1:50 AM, Steinhagelvoll wrote:
> It seems that the ini doesn't work properly. Every value seems to be nan.
>
> ini(A);
> ini(B);
> ini(C);
> writeln(A[0][0]);
> writeln(C[3][9]);
>
> nan
> nan

My bad, fixed:

double[1000][] A, B, C;

void main() {
         A = new double[1000][1000];
         B = new double[1000][1000];
         C = new double[1000][1000];

         import std.conv : to;
         import std.datetime;
         import std.stdio : writeln;

         ini(A);
         ini(B);
         ini(C);

         auto r = benchmark!run_test(10000);
         auto res = to!Duration(r[0]);
         writeln(res);
}

void run_test() {
         MatMul(A, B, C);
}

void ini(T)(T mtx) {
         foreach(ref v; mtx) {
                 v = 3.4;
         }

         foreach(i, v; mtx) {
                 foreach(j, ref vv; v) {
                         vv += (i * j) + (0.6 * j);
                 }
         }
}

void MatMul(T)(T A, T B, T C) {
         foreach(cv; C) {
                 cv = 0f;
         }

         foreach(i, cv; C) {
                 foreach(j, av; A[i]) {
                         auto bv = B[j];
                         foreach(k, cvv; cv) {
                                 cvv += av * bv[k];
                         }
                 }
         }

}


More information about the Digitalmars-d-learn mailing list