gfor on 4d matrix

[Old posts from the commercial version of ArrayFire] Discussion of ArrayFire using CUDA or OpenCL.

Moderator: pavanky

gfor on 4d matrix

Postby rm9 » Thu Jan 30, 2014 12:40 pm

Hi,

I'm trying to use gfor on a 4d matrix it doesn't seem to compile.
My original code is:
Code: Select all
for (int j = 0; j < output_maps; ++j) {
          output(span, span, span, j) = ConvolveValid3d2d(data, W(span, span, j));
         output(span, span, span, j) += b(j);
}


This compiles and runs well. (ConvolveValid3d2d calls convolve and cuts parts of the output to leave only a 'valid' convolution, no conditions).
The code with gfor doesn't compile:
Code: Select all
gfor (array j, output_maps) {
         output(span, span, span, j) = ConvolveValid3d2d(data, W(span, span, j));
         output(span, span, span, j) += b(j);
}


I get an error:
Code: Select all
Error   5   error C2664: 'af::array af::array::operator ()(int) const' : cannot convert argument 1 from 'af::seq' to 'const int'   C:\Users\Ran\Documents\Visual Studio 2012\Projects\EegNet3\EegNet3\ConvLayer.cpp   75


It seems to compile for a 3d matrix, so is this a known limitation of gfor? I couldn't find anything in the documentation.
Thanks.
rm9
 
Posts: 54
Joined: Thu Jan 30, 2014 5:44 am

Re: gfor on 4d matrix

Postby shehzan » Thu Jan 30, 2014 3:45 pm

Hi

The reason for that error is that there is currently no indexing function that takes in (seq, seq, seq, array).
We have added that now and will be pushing out a release soon.

As an alternate approach you can do this:
Code: Select all
data_ = moddims(data, data.dims(0) * data.dims(1), data.dims(2), data.dims(3));
W_ = moddims(W, W.dims(0) * W.dims(1), W.dims(2), W.dims(3));                  //If W is 4 dimensional
//Make output, b the same size as data_
gfor(array j, output_maps) {
    output(span, span, i) = ConvolveValid3d2d(data_, W(span, span, j), data.dims());
    output += b(j);
}
output = moddims(output, data.dims());

In the function do this:
Code: Select all
array data = moddims(data_, data.dims());
//Same for W if needed.
//Compute
//change the return value to data_.dims() using moddims


Moddims is a noop. So we are not wasting any computation in changing the dimensions in the manner described.
Please let me know if you have any questions about this approach.
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: gfor on 4d matrix

Postby rm9 » Thu Jan 30, 2014 4:48 pm

Thanks Shehzan but this doesn't work
By reshaping the kernel filter to 1d, it produces different results than the 2d convolution because the filter can "move" to positions in the data matrix it couldn't before.
It's great to know that the new indexing function is added.
When is the new version expected to be released?
Thanks!
rm9
 
Posts: 54
Joined: Thu Jan 30, 2014 5:44 am

Re: gfor on 4d matrix

Postby shehzan » Thu Jan 30, 2014 4:54 pm

Did you try to reshape it back to 2D in the function?
Here is a small example of how I tested the code. Most of it is hard-coded, but you can modify it to use variables very easily.
Code: Select all
array add(array a, array b) {                                                                                                                                                                                                             
    array a_ = moddims(a, 3, 3, 3);
    array b_ = moddims(b, 3, 3, 3);
    array c_ = a_ + b_;
    return moddims(c_, 9, 3);
}

In main:
Code: Select all
array t1_ = randu(3, 3, 3, 2);
array t2_ = randu(3, 3, 3, 2);
array t1 = moddims(t1_, 9, 3, 2);
array t2 = moddims(t2_, 9, 3, 2);

array t3 = randu(9, 3, 2);
gfor(array i, t3.dims(2)) {
        t3(span, span, i) = add(t1(span, span, i), t2(span, span, i));
}
t3 = moddims(t3, 3, 3, 3, 2);


To make sure this code works, you can use a for-loop (instead of gfor) to compute the correct result (using the indexing that you had shown in your original post) and compare it with the result of this code.
I expect both should be the same.
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: gfor on 4d matrix

Postby rm9 » Thu Jan 30, 2014 6:37 pm

Ok, I understood now. Seems to work, thanks!
I have a few more places where this is happening so I'm looking forward for the upcoming release.
Thanks agian.
rm9
 
Posts: 54
Joined: Thu Jan 30, 2014 5:44 am


Return to [archive-commercial] Programming & Development with ArrayFire

cron