Apply .clangformat to all repo source files
Change-Id: I7e79c6058f0303f9a98911e3b7dd2e8596079344
This commit is contained in:
@@ -27,39 +27,31 @@ THE SOFTWARE.
|
||||
#include "hip/hip_runtime.h"
|
||||
#include "test_common.h"
|
||||
#include <vector>
|
||||
unsigned p_streams =16;
|
||||
int p_repeat = 10;
|
||||
int p_db = 0;
|
||||
unsigned p_streams = 16;
|
||||
int p_repeat = 10;
|
||||
int p_db = 0;
|
||||
|
||||
|
||||
template <typename T>
|
||||
__global__ void
|
||||
vectorADDRepeat(hipLaunchParm lp,
|
||||
const T *A_d,
|
||||
const T *B_d,
|
||||
T *C_d,
|
||||
size_t NELEM,
|
||||
int repeat)
|
||||
{
|
||||
__global__ void vectorADDRepeat(hipLaunchParm lp, const T* A_d, const T* B_d, T* C_d, size_t NELEM,
|
||||
int repeat) {
|
||||
size_t offset = (blockIdx.x * blockDim.x + threadIdx.x);
|
||||
size_t stride = blockDim.x * gridDim.x ;
|
||||
size_t stride = blockDim.x * gridDim.x;
|
||||
|
||||
for (int j=1; j<=repeat;j++) {
|
||||
for (size_t i=offset; i<NELEM; i+=stride) {
|
||||
C_d[i] = A_d[i]*j + B_d[i]*j;
|
||||
for (int j = 1; j <= repeat; j++) {
|
||||
for (size_t i = offset; i < NELEM; i += stride) {
|
||||
C_d[i] = A_d[i] * j + B_d[i] * j;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//------
|
||||
// Structure for one stream - includes the stream + data buffers that are used by the stream.
|
||||
template <typename T>
|
||||
class Streamer {
|
||||
public:
|
||||
Streamer(size_t numElements, bool useNullStream=false);
|
||||
public:
|
||||
Streamer(size_t numElements, bool useNullStream = false);
|
||||
~Streamer();
|
||||
void enqueAsync();
|
||||
void queryUntilComplete();
|
||||
@@ -69,26 +61,24 @@ public:
|
||||
void D2H();
|
||||
|
||||
|
||||
public:
|
||||
T *_A_h;
|
||||
T *_B_h;
|
||||
T *_C_h;
|
||||
public:
|
||||
T* _A_h;
|
||||
T* _B_h;
|
||||
T* _C_h;
|
||||
|
||||
T *_A_d;
|
||||
T *_B_d;
|
||||
T *_C_d;
|
||||
T* _A_d;
|
||||
T* _B_d;
|
||||
T* _C_d;
|
||||
|
||||
hipStream_t _stream;
|
||||
hipEvent_t _event;
|
||||
hipEvent_t _event;
|
||||
|
||||
size_t _numElements;
|
||||
size_t _numElements;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Streamer<T>::Streamer(size_t numElements, bool useNullStream) :
|
||||
_numElements(numElements)
|
||||
{
|
||||
HipTest::initArrays (&_A_d, &_B_d, &_C_d, &_A_h, &_B_h, &_C_h, numElements, true);
|
||||
Streamer<T>::Streamer(size_t numElements, bool useNullStream) : _numElements(numElements) {
|
||||
HipTest::initArrays(&_A_d, &_B_d, &_C_d, &_A_h, &_B_h, &_C_h, numElements, true);
|
||||
|
||||
if (useNullStream) {
|
||||
_stream = 0x0;
|
||||
@@ -98,82 +88,65 @@ Streamer<T>::Streamer(size_t numElements, bool useNullStream) :
|
||||
HIPCHECK(hipEventCreate(&_event));
|
||||
|
||||
H2D();
|
||||
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void Streamer<T>::H2D()
|
||||
{
|
||||
HIPCHECK(hipMemcpy(_A_d, _A_h, _numElements*sizeof(T), hipMemcpyHostToDevice));
|
||||
HIPCHECK(hipMemcpy(_B_d, _B_h, _numElements*sizeof(T), hipMemcpyHostToDevice));
|
||||
void Streamer<T>::H2D() {
|
||||
HIPCHECK(hipMemcpy(_A_d, _A_h, _numElements * sizeof(T), hipMemcpyHostToDevice));
|
||||
HIPCHECK(hipMemcpy(_B_d, _B_h, _numElements * sizeof(T), hipMemcpyHostToDevice));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Streamer<T>::D2H()
|
||||
{
|
||||
HIPCHECK(hipMemcpy(_C_h, _C_d, _numElements*sizeof(T), hipMemcpyDeviceToHost));
|
||||
void Streamer<T>::D2H() {
|
||||
HIPCHECK(hipMemcpy(_C_h, _C_d, _numElements * sizeof(T), hipMemcpyDeviceToHost));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Streamer<T>::reset()
|
||||
{
|
||||
void Streamer<T>::reset() {
|
||||
HipTest::setDefaultData(_numElements, _A_h, _B_h, _C_h);
|
||||
H2D();
|
||||
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
void Streamer<T>::enqueAsync()
|
||||
{
|
||||
printf ("testing: %s numElements=%zu size=%6.2fMB\n", __func__, _numElements, _numElements * sizeof(T) / 1024.0/1024.0);
|
||||
void Streamer<T>::enqueAsync() {
|
||||
printf("testing: %s numElements=%zu size=%6.2fMB\n", __func__, _numElements,
|
||||
_numElements * sizeof(T) / 1024.0 / 1024.0);
|
||||
unsigned blocks = HipTest::setNumBlocks(blocksPerCU, threadsPerBlock, _numElements);
|
||||
hipLaunchKernel(
|
||||
vectorADDRepeat,
|
||||
dim3(blocks),
|
||||
dim3(threadsPerBlock),
|
||||
0,
|
||||
_stream,
|
||||
static_cast<const T*>(_A_d),
|
||||
static_cast<const T*>(_B_d),
|
||||
_C_d,
|
||||
_numElements,
|
||||
p_repeat);
|
||||
|
||||
hipLaunchKernel(vectorADDRepeat, dim3(blocks), dim3(threadsPerBlock), 0, _stream,
|
||||
static_cast<const T*>(_A_d), static_cast<const T*>(_B_d), _C_d, _numElements,
|
||||
p_repeat);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Streamer<T>::queryUntilComplete()
|
||||
{
|
||||
void Streamer<T>::queryUntilComplete() {
|
||||
int numQueries = 0;
|
||||
hipError_t e = hipSuccess;
|
||||
do {
|
||||
numQueries++;
|
||||
e = hipStreamQuery(_stream);
|
||||
} while (e != hipSuccess) ;
|
||||
} while (e != hipSuccess);
|
||||
|
||||
printf ("completed after %d queries\n", numQueries);
|
||||
printf("completed after %d queries\n", numQueries);
|
||||
};
|
||||
|
||||
|
||||
|
||||
//---
|
||||
//Parse arguments specific to this test.
|
||||
void parseMyArguments(int argc, char *argv[])
|
||||
{
|
||||
// Parse arguments specific to this test.
|
||||
void parseMyArguments(int argc, char* argv[]) {
|
||||
int more_argc = HipTest::parseStandardArguments(argc, argv, false);
|
||||
|
||||
// parse args for this test:
|
||||
for (int i = 1; i < more_argc; i++) {
|
||||
const char *arg = argv[i];
|
||||
const char* arg = argv[i];
|
||||
|
||||
if (!strcmp(arg, "--streams")) {
|
||||
if (++i >= argc || !HipTest::parseUInt(argv[i], &p_streams)) {
|
||||
failed("Bad streams argument");
|
||||
failed("Bad streams argument");
|
||||
}
|
||||
} else if (!strcmp(arg, "--repeat") || (!strcmp(arg, "-r"))) {
|
||||
if (++i >= argc || !HipTest::parseInt(argv[i], &p_repeat)) {
|
||||
failed("Bad repeat argument");
|
||||
failed("Bad repeat argument");
|
||||
}
|
||||
} else {
|
||||
failed("Bad argument '%s'", arg);
|
||||
@@ -182,70 +155,61 @@ void parseMyArguments(int argc, char *argv[])
|
||||
};
|
||||
|
||||
|
||||
void
|
||||
printBuffer(std::string name, int *f, size_t numElements)
|
||||
{
|
||||
void printBuffer(std::string name, int* f, size_t numElements) {
|
||||
std::cout << name << "\n";
|
||||
for (size_t i=0; i<numElements; i++) {
|
||||
printf ("%5zu: %d\n", i, f[i]);
|
||||
for (size_t i = 0; i < numElements; i++) {
|
||||
printf("%5zu: %d\n", i, f[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//---
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char* argv[]) {
|
||||
HipTest::parseStandardArguments(argc, argv, false);
|
||||
parseMyArguments(argc, argv);
|
||||
|
||||
typedef Streamer<int> IntStreamer;
|
||||
|
||||
std::vector<IntStreamer *> streamers;
|
||||
std::vector<IntStreamer*> streamers;
|
||||
|
||||
size_t numElements = N;
|
||||
|
||||
int *expected_H = (int*)malloc(numElements*sizeof(int));
|
||||
int* expected_H = (int*)malloc(numElements * sizeof(int));
|
||||
|
||||
|
||||
auto nullStreamer = new IntStreamer(numElements, true);
|
||||
|
||||
// Expected resultr - last streamer runs vectorADDRepeat, then nullstreamer adds lastStreamer->_C_d + lastStreamer->_C_d
|
||||
for (size_t i=0; i<numElements; i++) {
|
||||
expected_H[i] = ((nullStreamer->_A_h[i])*p_repeat + (nullStreamer->_B_h[i]) * p_repeat) *2;
|
||||
// Expected resultr - last streamer runs vectorADDRepeat, then nullstreamer adds
|
||||
// lastStreamer->_C_d + lastStreamer->_C_d
|
||||
for (size_t i = 0; i < numElements; i++) {
|
||||
expected_H[i] =
|
||||
((nullStreamer->_A_h[i]) * p_repeat + (nullStreamer->_B_h[i]) * p_repeat) * 2;
|
||||
}
|
||||
|
||||
|
||||
for (int i=0; i<p_streams; i++) {
|
||||
IntStreamer * s = new IntStreamer(numElements);
|
||||
for (int i = 0; i < p_streams; i++) {
|
||||
IntStreamer* s = new IntStreamer(numElements);
|
||||
streamers.push_back(s);
|
||||
}
|
||||
unsigned blocks = HipTest::setNumBlocks(blocksPerCU, threadsPerBlock, numElements);
|
||||
|
||||
for (int s=1; s<p_streams; s++) {
|
||||
if (p_tests & (1<<s)) {
|
||||
printf ("==> Test %x runAsnc, #streams=%d\n", (1<<s), s);
|
||||
for (int s = 1; s < p_streams; s++) {
|
||||
if (p_tests & (1 << s)) {
|
||||
printf("==> Test %x runAsnc, #streams=%d\n", (1 << s), s);
|
||||
nullStreamer->reset();
|
||||
|
||||
for (int i=0; i<s; i++) {
|
||||
for (int i = 0; i < s; i++) {
|
||||
streamers[i]->enqueAsync();
|
||||
}
|
||||
|
||||
auto lastStreamer = streamers[s - 1];
|
||||
|
||||
// Dispatch to NULL stream, should wait for prior async activity to complete before beginning:
|
||||
hipLaunchKernel(
|
||||
vectorADDRepeat,
|
||||
dim3(blocks),
|
||||
dim3(threadsPerBlock),
|
||||
0,
|
||||
0/*nullstream*/,
|
||||
static_cast<const int*>(lastStreamer->_C_d),
|
||||
static_cast<const int*>(lastStreamer->_C_d),
|
||||
nullStreamer->_C_d,
|
||||
numElements,
|
||||
1/*repeat*/);
|
||||
// Dispatch to NULL stream, should wait for prior async activity to complete before
|
||||
// beginning:
|
||||
hipLaunchKernel(vectorADDRepeat, dim3(blocks), dim3(threadsPerBlock), 0,
|
||||
0 /*nullstream*/, static_cast<const int*>(lastStreamer->_C_d),
|
||||
static_cast<const int*>(lastStreamer->_C_d), nullStreamer->_C_d,
|
||||
numElements, 1 /*repeat*/);
|
||||
|
||||
|
||||
if (p_db) {
|
||||
@@ -263,12 +227,12 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
|
||||
for (int s=1; s<p_streams; s+=2) {
|
||||
unsigned tmask = (0x10000 | (1<<s));
|
||||
for (int s = 1; s < p_streams; s += 2) {
|
||||
unsigned tmask = (0x10000 | (1 << s));
|
||||
if (p_tests & tmask) {
|
||||
nullStreamer->reset();
|
||||
printf ("==> Test %x runAsnc-odd-only, #streams=%d\n", tmask, s);
|
||||
for (int i=0; i<s; i++) {
|
||||
printf("==> Test %x runAsnc-odd-only, #streams=%d\n", tmask, s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
// RUn just odd streams so we have some empty ones to examine/optimize:
|
||||
if (i & 0x1) {
|
||||
streamers[i]->enqueAsync();
|
||||
@@ -276,18 +240,12 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
auto lastStreamer = streamers[s - 1];
|
||||
|
||||
// Dispatch to NULL stream, should wait for prior async activity to complete before beginning:
|
||||
hipLaunchKernel(
|
||||
vectorADDRepeat,
|
||||
dim3(blocks),
|
||||
dim3(threadsPerBlock),
|
||||
0,
|
||||
0/*nullstream*/,
|
||||
static_cast<const int*>(lastStreamer->_C_d),
|
||||
static_cast<const int*>(lastStreamer->_C_d),
|
||||
nullStreamer->_C_d,
|
||||
numElements,
|
||||
1/*repeat*/);
|
||||
// Dispatch to NULL stream, should wait for prior async activity to complete before
|
||||
// beginning:
|
||||
hipLaunchKernel(vectorADDRepeat, dim3(blocks), dim3(threadsPerBlock), 0,
|
||||
0 /*nullstream*/, static_cast<const int*>(lastStreamer->_C_d),
|
||||
static_cast<const int*>(lastStreamer->_C_d), nullStreamer->_C_d,
|
||||
numElements, 1 /*repeat*/);
|
||||
|
||||
nullStreamer->D2H();
|
||||
|
||||
@@ -298,26 +256,28 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
// Expected resultr - last streamer runs vectorADDRepeat
|
||||
for (size_t i=0; i<numElements; i++) {
|
||||
expected_H[i] = ((nullStreamer->_A_h[i])*p_repeat + (nullStreamer->_B_h[i]) * p_repeat);
|
||||
for (size_t i = 0; i < numElements; i++) {
|
||||
expected_H[i] = ((nullStreamer->_A_h[i]) * p_repeat + (nullStreamer->_B_h[i]) * p_repeat);
|
||||
}
|
||||
|
||||
if (p_tests & 0x20000) {
|
||||
|
||||
assert (p_streams >=2); // need a couple streams in order to run this test.
|
||||
assert(p_streams >= 2); // need a couple streams in order to run this test.
|
||||
nullStreamer->reset();
|
||||
printf ("\n==> Test hipStreamSynchronize with defaultStream \n");
|
||||
printf("\n==> Test hipStreamSynchronize with defaultStream \n");
|
||||
|
||||
// Enqueue a long-running job to stream1
|
||||
streamers[0]->enqueAsync();
|
||||
|
||||
// Check to see if synchronizing on a null stream synchronizes all other streams or just the null stream.
|
||||
// This function follows null stream semantics and will wait for all other blocking streams before returning.
|
||||
// This will wait on the host
|
||||
// Check to see if synchronizing on a null stream synchronizes all other streams or just the
|
||||
// null stream. This function follows null stream semantics and will wait for all other
|
||||
// blocking streams before returning. This will wait on the host
|
||||
HIPCHECK(hipStreamSynchronize(0));
|
||||
|
||||
// Copy with stream1, this could go async if the streamSync doesn't synchronize ALL the streams.
|
||||
HIPCHECK(hipMemcpyAsync(streamers[0]->_C_h, streamers[0]->_C_d, streamers[0]->_numElements*sizeof(int), hipMemcpyDeviceToHost, streamers[1]->_stream));
|
||||
// Copy with stream1, this could go async if the streamSync doesn't synchronize ALL the
|
||||
// streams.
|
||||
HIPCHECK(hipMemcpyAsync(streamers[0]->_C_h, streamers[0]->_C_d,
|
||||
streamers[0]->_numElements * sizeof(int), hipMemcpyDeviceToHost,
|
||||
streamers[1]->_stream));
|
||||
|
||||
|
||||
HIPCHECK(hipDeviceSynchronize());
|
||||
|
||||
Reference in New Issue
Block a user