Improve copy testing implementation.

- add tests for (unpinned/pinned) x H2H x D2D.
- Free memory at end of test.


[ROCm/clr commit: 1128610801]
This commit is contained in:
Ben Sander
2016-02-12 17:39:44 -06:00
parent 89e461988e
commit 4dfe77a99b
4 changed files with 214 additions and 34 deletions
+96 -9
View File
@@ -23,24 +23,21 @@ THE SOFTWARE.
#include "test_common.h"
int main(int argc, char *argv[])
// Test simple H2D copies and back.
void simpleTest1()
{
HipTest::parseStandardArguments(argc, argv, true);
printf ("test: %s\n", __func__);
size_t Nbytes = N*sizeof(int);
printf ("N=%zu Nbytes=%6.2fMB\n", N, Nbytes/1024.0/1024.0);
int *A_d, *B_d, *C_d;
int *A_h, *B_h, *C_h;
HipTest::initArrays (&A_d, &B_d, &C_d, &A_h, &B_h, &C_h, N);
HipTest::initArrays (&A_d, &B_d, &C_d, &A_h, &B_h, &C_h, N, false);
printf ("A_d=%p B_d=%p C_d=%p A_h=%p B_h=%p C_h=%p\n", A_d, B_d, C_d, A_h, B_d, C_h);
unsigned blocks = HipTest::setNumBlocks(blocksPerCU, threadsPerBlock, N);
HIPCHECK ( hipMemcpy(A_d, A_h, Nbytes, hipMemcpyHostToDevice));
HIPCHECK ( hipMemcpy(B_d, B_h, Nbytes, hipMemcpyHostToDevice));
@@ -50,8 +47,98 @@ int main(int argc, char *argv[])
HIPCHECK (hipDeviceSynchronize());
HipTest::checkVectorADD(A_h, B_h, C_h, N);
HipTest::freeArrays (A_d, B_d, C_d, A_h, B_h, C_h, false);
HIPCHECK (hipDeviceReset());
printf (" %s success\n", __func__);
}
// Test many different kinds of memory copies:
template <typename T>
void memcpyKind(bool usePinnedHost, bool useHostToHost, bool useMemkindDefault)
{
printf ("test: %s\n", __func__);
T *A_d, *B_d, *C_d;
T *A_h, *B_h, *C_h;
size_t Nbytes = N*sizeof(T);
HipTest::initArrays (&A_d, &B_d, &C_d, &A_h, &B_h, &C_h, N, usePinnedHost);
unsigned blocks = HipTest::setNumBlocks(blocksPerCU, threadsPerBlock, N);
T *A_hh = NULL;
T *B_hh = NULL;
T *C_dd = NULL;
// Allocate some extra arrays:
HIPCHECK ( hipMalloc(&C_dd, Nbytes) );
if (useHostToHost) {
if (usePinnedHost) {
HIPCHECK ( hipMallocHost(&A_hh, Nbytes) );
HIPCHECK ( hipMallocHost(&B_hh, Nbytes) );
} else {
A_hh = (T*)malloc(Nbytes);
B_hh = (T*)malloc(Nbytes);
}
// Do some extra host copies here to mix things up:
HIPCHECK ( hipMemcpy(A_hh, A_h, Nbytes, useMemkindDefault? hipMemcpyDefault : hipMemcpyHostToHost));
HIPCHECK ( hipMemcpy(B_hh, B_h, Nbytes, useMemkindDefault? hipMemcpyDefault : hipMemcpyHostToHost));
HIPCHECK ( hipMemcpy(A_d, A_hh, Nbytes, useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToDevice));
HIPCHECK ( hipMemcpy(B_d, B_hh, Nbytes, useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToDevice));
} else {
HIPCHECK ( hipMemcpy(A_d, A_h, Nbytes, useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToDevice));
HIPCHECK ( hipMemcpy(B_d, B_h, Nbytes, useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToDevice));
}
hipLaunchKernel(HipTest::vectorADD, dim3(blocks), dim3(threadsPerBlock), 0, 0, A_d, B_d, C_d, N);
#if 0
// Do some extra host copies here to mix things up:
HIPCHECK ( hipMemcpy(C_dd, C_d, Nbytes, useMemkindDefault? hipMemcpyDefault : hipMemcpyHostToHost));
//Destroy the original C_d:
HIPCHECK ( hipMemset(C_d, 0x5A, Nbytes));
HIPCHECK ( hipMemcpy(C_h, C_dd, Nbytes, useMemkindDefault? hipMemcpyDefault:hipMemcpyDeviceToHost));
#else
HIPCHECK ( hipMemcpy(C_h, C_d, Nbytes, useMemkindDefault? hipMemcpyDefault:hipMemcpyDeviceToHost));
#endif
HIPCHECK ( hipDeviceSynchronize() );
HipTest::checkVectorADD(A_h, B_h, C_h, N);
HipTest::freeArrays (A_d, B_d, C_d, A_h, B_h, C_h, usePinnedHost);
HIPCHECK ( hipDeviceReset() );
printf (" %s success\n", __func__);
}
int main(int argc, char *argv[])
{
HipTest::parseStandardArguments(argc, argv, true);
simpleTest1();
memcpyKind<float>(false, false, false);
memcpyKind<float>(true, false, false);
//memcpyKind<float>(true);
passed();
}
+62 -8
View File
@@ -96,7 +96,7 @@ vectorADD(hipLaunchParm lp,
template <typename T>
void initArrays(T **A_d, T **B_d, T **C_d,
T **A_h, T **B_h, T **C_h,
size_t N)
size_t N, bool usePinnedHost=false)
{
size_t Nbytes = N*sizeof(T);
@@ -110,14 +110,32 @@ void initArrays(T **A_d, T **B_d, T **C_d,
HIPCHECK ( hipMalloc(C_d, Nbytes) );
}
if (A_h)
*A_h = (T*)malloc(Nbytes);
if (B_h)
*B_h = (T*)malloc(Nbytes);
if (usePinnedHost) {
if (A_h) {
HIPCHECK ( hipMallocHost(A_h, Nbytes) );
}
if (B_h) {
HIPCHECK ( hipMallocHost(B_h, Nbytes) );
}
if (C_h) {
HIPCHECK ( hipMallocHost(C_h, Nbytes) );
}
} else {
if (A_h) {
*A_h = (T*)malloc(Nbytes);
HIPASSERT(*A_h != NULL);
}
if (B_h) {
*B_h = (T*)malloc(Nbytes);
HIPASSERT(*B_h != NULL);
}
if (C_h)
*C_h = (T*)malloc(Nbytes);
if (C_h) {
*C_h = (T*)malloc(Nbytes);
HIPASSERT(*C_h != NULL);
}
}
// Initialize the host data:
@@ -130,7 +148,43 @@ void initArrays(T **A_d, T **B_d, T **C_d,
}
template <typename T>
void freeArrays(T *A_d, T *B_d, T *C_d,
T *A_h, T *B_h, T *C_h, bool usePinnedHost)
{
if (A_d) {
HIPCHECK ( hipFree(A_d) );
}
if (B_d) {
HIPCHECK ( hipFree(B_d) );
}
if (C_d) {
HIPCHECK ( hipFree(C_d) );
}
if (usePinnedHost) {
if (A_h) {
HIPCHECK (hipFreeHost(A_h));
}
if (B_h) {
HIPCHECK (hipFreeHost(B_h));
}
if (C_h) {
HIPCHECK (hipFreeHost(C_h));
}
} else {
if (A_h) {
free (A_h);
}
if (B_h) {
free (B_h);
}
if (C_h) {
free (C_h);
}
}
}
// Assumes C_h contains vector add of A_h + B_h