Reference from: https://msdn.microsoft.com/en-us/library/hh873135.aspx
#include <iostream> #include <iomanip> #include <amp.h>using namespace Concurrency;const int ROWS = 8; const int COLS = 9;//Tilerow and Tilecolumn Specify the tile that each thread is in.//globalrow and Globalcolum Specify The location of the thread in the array_view.//localrow and Localcolumn Specify the location of the thread Relativie to The tile.struct description{int value;int tilerow;int tilecolumn;int globalrow;int globalcolumn;int localRow;int localcolumn;};/ /A helper function for formatting the output.void setconsolecolor (int color) {int ColorValue = (color = = 4)? 4:2; Setconsoletextattribute (GetStdHandle (std_output_handle), colorvalue);} A helper function for farmatting the output.void setconsolesize (int height, int width) {COORD COORD; COORD. X = width; Coord. Y = height; Setconsolescreenbuffersize (GetStdHandle (std_output_handle), coord); Small_rect *rect = new Small_rect (); rect->left = 0;rect->top = 0;rect->right = Width;rect->bottom = height; SEtconsolewindowinfo (GetStdHandle (Std_output_handle), true, rect);} This method creates an 8 x 9 matrix of Description structures. In the//-Parallel_for_each, the structure is updated with Tile,global, and local indices.void tilingdescription () { Create 8x9 Description structures.std::vector<description> descs;for (int i = 0; i < ROWS * COLS; i++) {Des Cription d = {i, 0, 0, 0, 0, 0, 0};d Escs.push_back (d);} Create an array_view from the Description structures.extent<2> matrix (ROWS, COLS); Array_view<description, 2& Gt Descriptions (matrix, Descs);//Update each Description with the tile, global. and local Indices.parallel_for_each (Descriptions.extent.tile<2, 3> (), [=] (tiled_index<2, 3> t_idx) Restrict (AMP) {descriptions[t_idx].globalrow = t_idx.global[0];d escriptions[t_idx].globalcolumn = t_idx.global[1]; Descriptions[t_idx].tilerow = t_idx.tile[0];d escriptions[t_idx].tilecolumn = t_idx.tile[1];d escriptions[t_idx]. Localrow = t_idx.local[0];d ESCRIptions[t_idx].localcolumn = t_idx.local[1];}); /Print out the Description structure for each element in the matrix.//Tiles is displayed in red and green to Distinguis h them from each of the other. Setconsolesize (+), for (int row = 0; row < ROWS; row++) {for (int column = 0; column < COLS; column++) {Setconso Lecolor (Descriptions (row, column). Tilerow + descriptions (row, column). Tilecolumn)% 2); Std::cout << "Value:" << STD::SETW (2) << descriptions (row, column). Value << "";} Std::cout << "\ n"; for (int column = 0; column < COLS; column++) {Setconsolecolor (descriptions (row, column). Tiler ow + descriptions (row, column). Tilecolumn)% 2) std::cout << "Tile:" << "(" << descriptions (row, column ). Tilerow << "," << descriptions (row, column). Tilecolumn << ")";} Std::cout << "\ n"; for (int column = 0; column < COLS; column++) {Setconsolecolor (descriptions (row, column). Tiler ow + descriptions (row, column). TilecolumN)% 2); Std::cout << "Global:" << "(" << descriptions (row, column). Globalrow << "," << desc Riptions (row, column). Globalcolumn << ")";} Std::cout << "\ n"; for (int column = 0; column < COLS; column++) {Setconsolecolor (descriptions (row, column). Tiler ow + descriptions (row, column). Tilecolumn)% 2) std::cout << "Local:" << "(" << descriptions (row, Colum n). Localrow << "," << descriptions (row, column). Localcolumn << ")";} Std::cout << "\ n"; std::cout << "\ n";}} #define SampleSize 2#define matrixsize 8void samplingexample () {//Create data and Array_view for the MATRIX.STD::VECTOR< ;float> rawdata;for (int i = 0; i < matrixsize * Matrixsize; i++) {Rawdata.push_back ((float) i);} Extent<2> dataextent (matrixsize, matrixsize); Array_view<float, 2> Matrix (dataextent, rawData);//Create The array for the averages.//there are one element in the output for each tile in the Data.std::vector<float> Outputdata;int outputsize = matrixsize/samplesize;for (int j = 0; J < outputsize * Outputsize; j + +) {Outputdata. Push_back ((float) 0);} Extent<2> outputextent (matrixsize/samplesize, matrixsize/samplesize); Array<float, 2> averages ( Outputextent, Outputdata.begin (), Outputdata.end ());//Use tiles that is samplesize x samplesize//Find the average of th E values in each tile.//the-reference-type variable you can pass into the parallel_fo_each_call//is a concurrency:: Array.parallel_for_each (Matrix.extent.tile<samplesize, samplesize> (), [=, &averages] (tiled_index< SampleSize, samplesize> t_idx) Restrict (AMP) {//Copy the values of the tile into a tile-sized array.tile_static float t ILEVALUES[SAMPLESIZE][SAMPLESIZE];TILEVALUES[T_IDX.LOCAL[0]][T_IDX.LOCAL[1]] = matrix[t_idx];//Wait for the Tile-sized array to load before you calculate the average.t_idx.barrier.wait ();/If you remove the IF statement and then the Calculation executes for every//thread In the tiles, and makes the same assignemnt to averages each time.if (t_idx.local[0] = = 0 && t_idx.local[1] = = 0) { for (int trow = 0; Trow < samplesize; trow++) {for (int tcol = 0; Tcol < samplesize; tcol++) {averages (t_idx.tile[0), T_IDX.TILE[1]) + = Tilevalues[trow][tcol];}} Averages (t_idx.tile[0], t_idx.tile[1])/= (float) (samplesize * samplesize);}); /Print out the results.//cannot access the values in aveages directly. You must copy them//Vack to a CPU variable.outputdata = averages;for (int row = 0, row < outputsize; row++) {for (int c OL = 0; Col < outputsize; col++) {std::cout << Outputdata[row * outputsize + col] << "";} Std::cout << "\ n";} Output for samplessize = 2 is://4.5 6.5 8.5 10.5//20.5 22.5 24.5 26.5//36.5 38.5 40.5 42.5//52.5 54.5 56.5 58.5/ /Output for samplesize = 4 is://13.5 17.5//45.5 49.5}void main () {//tilingdescription (); Samplingexample (); Char wait;std::cin >> wait;}
C++amp Matrix chunking