Created
June 3, 2014 16:18
-
-
Save piotrbla/56e12ff13598611bd5e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <amp.h> | |
#include <iostream> // For std::cout etc | |
#include <sys\timeb.h> | |
using namespace concurrency; // Save some typing :) | |
using std::vector; // Ditto. Comes from <vector> brought in by amp.h | |
void perform_calculation(vector<int>& vA, vector<int>& vB, vector<int>& vC, int M, int N) | |
{ | |
extent<2> e(M, N); | |
array_view<int, 2> a(e, vA), b(e, vB); | |
array_view<int, 2> c(e, vC); | |
struct timeb start, end; | |
int diff; | |
ftime(&start); | |
parallel_for_each(e, | |
[=](index<2> idx)restrict(amp) | |
{ | |
c[idx] = a[idx] + b[idx]; | |
} | |
); | |
ftime(&end); | |
diff = (int) (1000.0 * (end.time - start.time) | |
+ (end.millitm - start.millitm)); | |
std::cout << "Operation took..." << diff << "milliseconds" << std::endl; | |
} | |
void perform_calculation3(vector<int>& vA, vector<int>& vB, vector<int>& vC, int M, int N) | |
{ | |
extent<2> e(M, N); | |
array_view<int, 2> a(e, vA), b(e, vB); | |
array_view<int, 2> c(e, vC); | |
index<2> idx(0, 0); | |
for (idx[0] = 0; idx[0] < e[0]; idx[0]++) | |
{ | |
for (idx[1] = 0; idx[1] < e[1]; idx[1]++) | |
{ | |
c[idx] = a[idx] + b[idx]; | |
//c(idx[0], idx[1]) = a(idx[0], idx[1]) + b(idx[0], idx[1]); | |
} | |
} | |
} | |
void perform_calculation2(vector<int>& vA, vector<int>& vB, vector<int>& vC, int M, int N) | |
{ | |
array_view<int> a(M*N, vA), b(M*N, vB); | |
array_view<int> c(M*N, vC); | |
struct timeb start, end; | |
int diff; | |
ftime(&start); | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
c(i * N + j) = a(i * N + j) + b(i * N + j); | |
} | |
} | |
ftime(&end); | |
diff = (int) (1000.0 * (end.time - start.time) | |
+ (end.millitm - start.millitm)); | |
std::cout << "Operation took..." << diff << "milliseconds" << std::endl; | |
} | |
void perform_calculation1(vector<int>& vA, vector<int>& vB, vector<int>& vC, int M, int N) | |
{ | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
vC[i * N + j] = vA[i * N + j] + vB[i * N + j]; | |
} | |
} | |
} | |
void MatrixMultiplication1(vector<int>& vC, const vector<int>& vA, const vector<int>& vB, int M, int N, int W) | |
{ | |
for (int row = 0; row < M; row++) | |
{ | |
for (int col = 0; col < N; col++) | |
{ | |
int sum = 0; | |
for(int i = 0; i < W; i++) | |
sum += vA[row * W + i] * vB[i * N + col]; | |
vC[row * N + col] = sum; | |
} | |
} | |
} | |
void MatrixMultiplication2(vector<int>& vC, const vector<int>& vA, const vector<int>& vB, int M, int N, int W) | |
{ | |
array_view<const int, 2> a(M, W, vA), b(W, N, vB); | |
array_view<int, 2> c(M, N, vC); | |
c.discard_data(); | |
parallel_for_each(c.extent, [=](index<2> idx) restrict(amp) | |
{ | |
int row = idx[0]; int col = idx[1]; | |
int sum = 0; | |
for(int i = 0; i < b.extent[0]; i++) | |
sum += a(row, i) * b(i, col); | |
c[idx] = sum; | |
}); | |
c.synchronize(); | |
} | |
void do_it() | |
{ | |
// Rows and columns for matrix | |
const int M = 4096; | |
const int N = 4096; | |
//const int M = 1024; | |
//const int N = 1024; | |
// Create storage for a matrix of above size | |
vector<int> vA(M * N); | |
vector<int> vB(M * N); | |
// Populate matrix objects | |
int i = 0; | |
//std::generate(vA.begin(), vA.end(), [&i](){return i++;}); | |
//std::generate(vB.begin(), vB.end(), [&i](){return i--;}); | |
std::generate(vA.begin(), vA.end(), [&i](){return (double)rand() / (RAND_MAX + 1) * (8);}); | |
std::generate(vB.begin(), vB.end(), [&i](){return (double)rand() / (RAND_MAX + 1) * (8);}); | |
// Output storage for matrix calculation | |
vector<int> vC(M * N); | |
struct timeb start, end; | |
int diff; | |
ftime(&start); | |
//perform_calculation(vA, vB, vC, M, N); | |
MatrixMultiplication2(vC, vA, vB, M, N, M); | |
//MatrixMultiplication1(vC, vA, vB, M, N, M); | |
ftime(&end); | |
diff = (int) (1000.0 * (end.time - start.time) | |
+ (end.millitm - start.millitm)); | |
std::cout << "Operation took..." << diff << "milliseconds" << std::endl; | |
std::cout << "Nie wierze: " << vC[1] << std::endl; | |
std::cout << "Nie wierze: " << vA[1] << std::endl; | |
std::cout << "Nie wierze: " << vB[1] << std::endl; | |
} | |
int main() | |
{ | |
do_it(); | |
std::cout << "Hit any key to exit..." << std::endl; | |
std::cin.get(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment