locked
c++ amp - using tiles, is it correct? help please(bottom post) RRS feed

  • Question

  • EDIT: check bottom post, please!

    hi,


    i was wandering if you could help me with my C++ amp code. I want to parallel the multiplication of vector and matrix. Here is my code

    #include <amp.h>

    using namespace std;
    using namespace concurrency;

    int _tmain(int argc, _TCHAR* argv[])

    int n;
        cin>> n;
    double **A = new double*[n];
        for(int i = 0; i < n; ++i) {
            A[i] = new double[n];
        }

    for (int i = 0; i < n ; ++ i)
            for (int j = 0; j < n; ++ j){

                A[i][j]=rand()%100;
                A[j][i]=A[i][j];

            }

    for( int i=0; i < n; i++)
                x[i]=0;
            double *ri = new double[n];
            for( int i=0; i < n; i++)
                ri[i]=0;
            double *ri1 = new double[n];

    array_view<const double, 2> a(n, n, A);
    array_view<const double, 1> X(n, x);
    array_view<double, 1> RI(n, ri);
        RI.discard_data();
    parallel_for_each(
        RI.extent,
        [=] (index<1> idx) restrict(amp) {
            int y = idx[0];

        double result = 0.0f;
                for (int k=0; k<n; k++) {
                    result += a(y, k) * X(k); }
                RI[idx] = result;
    });
    RI.synchronize();


    This, however, does raise the errors C2228 and C2338, which I've not been able to resolve so far.

    Thank you in advance!
    Regards,
    Bartosz






    • Edited by maniabarti Sunday, January 18, 2015 3:09 AM
    Monday, January 12, 2015 9:20 PM

All replies

  • I get to know that it is impossible for c++ amp to handle dynamic arrays because an input for an array_view is that it is not dense.

    But there are the same errors right here:

    #include <amp.h>
    #include <vector>
    #include "stdafx.h"
    
    using namespace concurrency;
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    int const n = 500;
    
    
    double A[n][n];
    
    for (int i = 0; i < n ; ++ i)
      for (int j = 0; j < n; ++ j){
        A[i][j]=rand()%100;
        A[j][i]=A[i][j];
      }
      double x[n];
    for(int i=0; i < n; i++)
      x[i]=0;
    double ri[n];
    for(int i=0; i < n; i++)
      ri[i]=0;
    double ri1[n];
    
    array_view<const double, 2> a(n, n, A);
    array_view<const double, 1> X(n, x);
    array_view<double, 1> RI(n, ri);
    RI.discard_data();
    parallel_for_each(
      RI.extent,
      [=] (index<1> idx) restrict(amp) {
        int y = idx[0];
        double result = 0.0f;
        for (int k=0; k<n; k++) {
          result += a(y, k) * X(k); }
          RI[idx] = result;
      }
    );
    RI.synchronize();
    
    }

    with errors:

    1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
    1>  ConsoleApplication1.cpp
    1>c:\users\bakik\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(27): warning C4101: 'ri1' : unreferenced local variable
    1>d:\visualstudio\vc\include\amp.h(2892): error C2228: left of '.data' must have class/struct/union
    1>          type is 'const double [500][500]'
    1>          c:\users\bakik\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(29) : see reference to function template instantiation 'Concurrency::array_view<_Value_type,_Rank>::array_view<double[500][500]>(int,int,const _Container (&))' being compiled
    1>          with
    1>          [
    1>              _Value_type=const double,
    1>              _Rank=2,
    1>              _Container=double [500][500]
    1>          ]
    1>          c:\users\bakik\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(29) : see reference to function template instantiation 'Concurrency::array_view<_Value_type,_Rank>::array_view<double[500][500]>(int,int,const _Container (&))' being compiled
    1>          with
    1>          [
    1>              _Value_type=const double,
    1>              _Rank=2,
    1>              _Container=double [500][500]
    1>          ]
    1>d:\visualstudio\vc\include\amp.h(2895): error C2228: left of '.data' must have class/struct/union
    1>          type is 'const double [500][500]'
    1>d:\visualstudio\vc\include\amp.h(2895): error C2338: container element type and array view element type must match
    1>d:\visualstudio\vc\include\amp.h(2897): error C2228: left of '.size' must have class/struct/union
    1>          type is 'const double [500][500]'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    

    Could you help me, please?

    Tuesday, January 13, 2015 7:23 PM
  • In order to solve the compilation problem, use a 1-dimensional array, a std::vector or std::array. If the size is large, then use vector:

    std::vector<double> A( n * n );

    array_view<double, 2> a( n, n, A );

    for( int i = 0; i < n; ++i )

    {

           for( int j = 0; j < n; ++j )

           {

                  a[i][j] = rand() % 100;
                  . . . .

    Or fill A directly using a single loop.

    Then check the correctness of the rest.

    By the way, the #include directives have to be moved to stdafx.h.


    • Edited by Viorel_MVP Wednesday, January 14, 2015 7:41 AM
    Wednesday, January 14, 2015 7:38 AM
  • is that code correct? could someone answet ASAP? I have to finish my project today.

    #include <amp.h>
    #include <vector>
    #include "stdafx.h"
    #include <iostream>
    
    using namespace concurrency;
    using namespace std;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	int n = 500;
    
    	vector<float> A(n*n);
    	for (int i = 0; i < n*n ; ++ i)
    		A[i]=rand()%100;
    
    	array_view<float, 2> a(n, n, A);
    
    	vector<float> x(n);
    
    	for(int i=0; i < n; i++)
    		x[i]=0;
    	array_view<float, 1> X(n, x);
    
    	vector<float> M_1(n*n);
    
    	for (int i = 0; i < n; ++ i){
    		for (int j = 0; j < n; ++ j){
    			if (i==j) {
    				if(A[i*n+j]==0) 
    					M_1[i*n+j]=1;
    				else
    					M_1[i*n+j]=1/A[i*n+j]; 
    			}
    			else
    				M_1[i*n+j]=0;
    		}
    	}
    	array_view<float, 2> m_1(n, n, M_1);
    	static const int kaf = 10;
    	parallel_for_each(
    		a.extent.tile<kaf,kaf>(),
    		[=] (tiled_index<kaf,kaf> idx) restrict(amp) {
    
    			int y1 = idx.global[0];
    			int o1 = idx.global[1];
    			int y = idx.local[0];
    			int o = idx.local[1];
    			float result = 0.0f;
    			for (int i = 0; i<n; i +=kaf) {
    				tile_static float loc[kaf][kaf];
    				tile_static float loc2[kaf];
    				loc[y][o] = m_1(y1, o1);
    				loc2[o] = X(o1);
    				idx.barrier.wait();
    
    				for (int k=0; k<n; k++) 
    					result += m_1(y, k) * X(k); 
    				idx.barrier.wait(); } 
    
    			a[idx.global] = result;
    
    	}
    	);
    	a.synchronize();
    
    	system("PAUSE");
    }

    • Edited by maniabarti Sunday, January 18, 2015 3:10 AM
    Wednesday, January 14, 2015 4:59 PM
  • is that correct? check that, please!
    Sunday, January 18, 2015 3:10 AM
  • well, unfortunalety i haven't figured out why [n]*[n] x [n]*[n] arrays works fine, but when I want to product [n] * [n] x [n] it doesn't work..

    	parallel_for_each(
    		RI.extent.tile<kaf,kaf>(),
    		[=] (tiled_index<kaf,kaf> idx) restrict(amp) {
    
    			int y1 = idx.global[0];
    			int o1 = idx.global[1];
    			int y = idx.local[0];
    			int o = idx.local[1];
    			
    			float result = 0.0f;
    			//float result = 0;
    			for (int i = 0; i<n; i +=kaf) {
    				tile_static float loc[kaf][kaf];
    				tile_static float loc2[kaf];
    				loc[y][o] =	a(y1, o1+1);
    				loc2[o] = X(o1);
    				idx.barrier.wait();
    				for (int k=0; k< kaf; k++) {
    					result += loc[y][k] * loc2[k]; }
    				idx.barrier.wait(); } 
    		RI[idx.global] = result;
    	}
    	);
    		RI.synchronize();


    with errors like:

    Error 5 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'const Concurrency::index<_Rank>' (or there is no acceptable conversion) c:\users\bakik\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 141

    Error 12 error C2088: '[' : illegal for class c:\users\bakik\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 141

    Sunday, January 18, 2015 9:30 PM