Dosyalar
rocm-systems/samples/0_Intro/hcc_dialects/vadd_hc_am.cpp
T

65 satır
2.1 KiB
C++
Ham Normal Görünüm Geçmiş

2016-04-13 17:32:38 -05:00
// Simple test showing how to use HC syntax with AM (accelerator memory).
// AM provides a set of c-style memory management routines for allocating,
// freeing, and copying memory. am_alloc returns a device pointer
// which can only be used on the device. The programmer has full control
// over when data is copied.
#include <hc.hpp>
#include <hc_am.hpp>
int main(int argc, char *argv[])
{
int sizeElements = 1000000;
size_t sizeBytes = sizeElements * sizeof(float);
2016-05-03 14:32:59 +05:30
bool pass = true;
2016-04-13 17:32:38 -05:00
// Allocate host memory
float *A_h = (float*)malloc(sizeBytes);
float *B_h = (float*)malloc(sizeBytes);
float *C_h = (float*)malloc(sizeBytes);
// Allocate device pointers:
// Unlike array_view, these must be explicitly managed by user:
hc::accelerator acc; // grab default accelerator where we want to allocate memory:
hc::accelerator_view av = acc.get_default_view();
float *A_d, *B_d, *C_d;
A_d = hc::am_alloc(sizeBytes, acc, 0);
B_d = hc::am_alloc(sizeBytes, acc, 0);
C_d = hc::am_alloc(sizeBytes, acc, 0);
// Initialize host data
for (int i=0; i<sizeElements; i++) {
A_h[i] = 1.618f * i;
B_h[i] = 3.142f * i;
C_h[i] = 0;
2016-04-13 17:32:38 -05:00
}
av.copy(A_h, A_d, sizeBytes); // C++ copy H2D
av.copy(B_h, B_d, sizeBytes); // C++ copy H2D
2016-04-13 17:32:38 -05:00
// Launch kernel onto AV.
// Because the kernel PFE and the copies are submitted to same AV, they will execute in order
// and we don't need additional synchronization to ensure the copies complete before the PFE begins.
hc::completion_future cf=
2016-04-13 17:32:38 -05:00
hc::parallel_for_each(av, hc::extent<1> (sizeElements),
[=] (hc::index<1> idx) [[hc]] {
2016-04-13 17:32:38 -05:00
int i = idx[0];
C_d[i] = A_d[i] + B_d[i];
2016-04-13 17:32:38 -05:00
});
// This copy is in same AV as the kernel and thus will wait for the kernel to finish before executing.
av.copy(C_d, C_h, sizeBytes); // C++ copy D2H
2016-04-13 17:32:38 -05:00
for (int i=0; i<sizeElements; i++) {
float ref= 1.618f * i + 3.142f * i;
if (C_h[i] != ref) {
printf ("error:%d computed=%6.2f, reference=%6.2f\n", i, C_h[i], ref);
2016-05-03 14:32:59 +05:30
pass = false;
2016-04-13 17:32:38 -05:00
}
};
2016-05-03 14:32:59 +05:30
if (pass) printf ("PASSED!\n");
2016-04-13 17:32:38 -05:00
}