Merge branch 'amd-master-next' into amd-npi-next

Change-Id: Id97c6b8c875731250049a5b9dc8062311ae291ad


[ROCm/hip-tests commit: 172e6eb53f]
This commit is contained in:
Vlad Sytchenko
2020-05-07 17:17:21 -04:00
12 changed files with 1043 additions and 323 deletions
@@ -0,0 +1,114 @@
/*
Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "test_common.h"
#include <iostream>
#include <time.h>
/* HIT_START
* BUILD: %t %s ../../src/test_common.cpp EXCLUDE_HIP_PLATFORM nvcc
* TEST: %t
* HIT_END
*/
#define NUM_SIZE 19 //size up to 16M
#define NUM_ITER 500 //Total GPU memory up to 16M*500=8G
void valSet(int* A, int val, size_t size) {
size_t len = size / sizeof(int);
for (int i = 0; i < len; i++) {
A[i] = val;
}
}
void setup(size_t *size, const int num, int **pA) {
std::cout << "size: ";
for (int i = 0; i < num; i++) {
size[i] = 1 << (i + 6);
std::cout << size[i] << " ";
}
std::cout << std::endl;
*pA = (int*)malloc(size[num - 1]);
valSet(*pA, 1, size[num - 1]);
}
void testInit(size_t size, int *A) {
int *Ad;
clock_t start = clock();
hipMalloc(&Ad, size); //hip::init() will be called
clock_t end = clock();
double uS = (end - start) * 1000000. / CLOCKS_PER_SEC;
std::cout << "Initial" << std::endl;
std::cout << "hipMalloc(" << size << ") cost " << uS << "us" << std::endl;
start = clock();
hipMemcpy(Ad, A, size, hipMemcpyHostToDevice);
hipDeviceSynchronize();
end = clock();
uS = (end - start) * 1000000. / CLOCKS_PER_SEC;
std::cout << "hipMemcpy(" << size << ") cost " << uS << "us" << std::endl;
start = clock();
hipFree(Ad);
end = clock();
uS = (end - start) * 1000000. / CLOCKS_PER_SEC;
std::cout << "hipFree(" << size << ") cost " << uS << "us" << std::endl;
}
int main() {
double uS;
clock_t start, end;
size_t size[NUM_SIZE] = { 0 };
int *Ad[NUM_ITER] = { nullptr };
int *A;
setup(size, NUM_SIZE, &A);
testInit(size[0], A);
for (int i = 0; i < NUM_SIZE; i++) {
std::cout << size[i] << std::endl;
start = clock();
for (int j = 0; j < NUM_ITER; j++) {
HIPCHECK(hipMalloc(&Ad[j], size[i]));
}
end = clock();
uS = (end - start) * 1000000. / (NUM_ITER * CLOCKS_PER_SEC);
std::cout << "hipMalloc(" << size[i] << ") cost " << uS << "us" << std::endl;
start = clock();
for (int j = 0; j < NUM_ITER; j++) {
HIPCHECK(hipMemcpy(Ad[j], A, size[i], hipMemcpyHostToDevice));
}
hipDeviceSynchronize();
end = clock();
uS = (end - start) * 1000000. / (NUM_ITER * CLOCKS_PER_SEC);
std::cout << "hipMemcpy(" << size[i] << ") cost " << uS << "us" << std::endl;
start = clock();
for (int j = 0; j < NUM_ITER; j++) {
HIPCHECK(hipFree(Ad[j]));
Ad[j] = nullptr;
}
end = clock();
double uS = (end - start) * 1000000. / (NUM_ITER * CLOCKS_PER_SEC);
std::cout << "hipFree(" << size[i] << ") cost " << uS << "us" << std::endl;
}
free(A);
passed();
}
@@ -0,0 +1,281 @@
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <complex>
#include "timer.h"
#include "test_common.h"
/* HIT_START
* BUILD: %t %s ../../src/test_common.cpp timer.cpp EXCLUDE_HIP_PLATFORM nvcc
* TEST: %t
* HIT_END
*/
// Quiet pesky warnings
#ifdef WIN_OS
#define SNPRINTF sprintf_s
#else
#define SNPRINTF snprintf
#endif
#define NUM_SIZES 8
//4KB, 8KB, 64KB, 256KB, 1 MB, 4MB, 16 MB, 16MB+10
static const unsigned int Sizes[NUM_SIZES] = {4096, 8192, 65536, 262144, 1048576, 4194304, 16777216, 16777216+10};
static const unsigned int Iterations[2] = {1, 1000};
#define BUF_TYPES 4
// 16 ways to combine 4 different buffer types
#define NUM_SUBTESTS (BUF_TYPES*BUF_TYPES)
#define CHECK_RESULT(test, msg) \
if ((test)) \
{ \
printf("\n%s\n", msg); \
abort(); \
}
void setData(void *ptr, unsigned int size, char value)
{
char *ptr2 = (char *)ptr;
for (unsigned int i = 0; i < size ; i++)
{
ptr2[i] = value;
}
}
void checkData(void *ptr, unsigned int size, char value)
{
char *ptr2 = (char *)ptr;
for (unsigned int i = 0; i < size; i++)
{
if (ptr2[i] != value)
{
printf("Data validation failed at %d! Got 0x%08x\n", i, ptr2[i]);
printf("Expected 0x%08x\n", value);
CHECK_RESULT(true, "Data validation failed!");
break;
}
}
}
int main(int argc, char* argv[]) {
HipTest::parseStandardArguments(argc, argv, true);
hipError_t err = hipSuccess;
hipDeviceProp_t props = {0};
hipGetDeviceProperties(&props, p_gpuDevice);
CHECK_RESULT(err != hipSuccess, "hipGetDeviceProperties failed" );
printf("Set device to %d : %s\n", p_gpuDevice, props.name);
printf("Legend: unp - unpinned(malloc), hM - hipMalloc(device)\n");
printf(" hHR - hipHostRegister(pinned), hHM - hipHostMalloc(prePinned)\n");
err = hipSetDevice(p_gpuDevice);
CHECK_RESULT(err != hipSuccess, "hipSetDevice failed" );
unsigned int bufSize_;
bool hostMalloc[2] = {false};
bool hostRegister[2] = {false};
bool unpinnedMalloc[2] = {false};
unsigned int numIter;
void *memptr[2] = {NULL};
void *alignedmemptr[2] = {NULL};
void* srcBuffer = NULL;
void* dstBuffer = NULL;
int numTests = (p_tests == -1) ? (NUM_SIZES*NUM_SUBTESTS*2 - 1) : p_tests;
int test = (p_tests == -1) ? 0 : p_tests;
for(;test <= numTests; test++)
{
unsigned int srcTest = (test / NUM_SIZES) % BUF_TYPES;
unsigned int dstTest = (test / (NUM_SIZES*BUF_TYPES)) % BUF_TYPES;
bufSize_ = Sizes[test % NUM_SIZES];
hostMalloc[0] = hostMalloc[1] = false;
hostRegister[0] = hostRegister[1] = false;
unpinnedMalloc[0] = unpinnedMalloc[1] = false;
srcBuffer = dstBuffer = 0;
memptr[0] = memptr[1] = NULL;
alignedmemptr[0] = alignedmemptr[1] = NULL;
size_t width = static_cast<size_t>(sqrt(static_cast<float>(bufSize_)));
if (srcTest == 3)
{
hostRegister[0] = true;
}
else if (srcTest == 2)
{
hostMalloc[0] = true;
}
else if (srcTest == 1)
{
unpinnedMalloc[0] = true;
}
if (dstTest == 1)
{
unpinnedMalloc[1] = true;
}
else if (dstTest == 2)
{
hostMalloc[1] = true;
}
else if (dstTest == 3)
{
hostRegister[1] = true;
}
numIter = Iterations[test / (NUM_SIZES * NUM_SUBTESTS)];
if (hostMalloc[0])
{
err = hipHostMalloc((void**)&srcBuffer, bufSize_, 0);
setData(srcBuffer, bufSize_, 0xd0);
CHECK_RESULT(err != hipSuccess, "hipHostMalloc failed");
}
else if (hostRegister[0])
{
memptr[0] = malloc(bufSize_ + 4096);
alignedmemptr[0] = (void*)(((size_t)memptr[0] + 4095) & ~4095);
srcBuffer = alignedmemptr[0];
setData(srcBuffer, bufSize_, 0xd0);
err = hipHostRegister(srcBuffer, bufSize_, 0);
CHECK_RESULT(err != hipSuccess, "hipHostRegister failed");
}
else if (unpinnedMalloc[0])
{
memptr[0] = malloc(bufSize_ + 4096);
alignedmemptr[0] = (void*)(((size_t)memptr[0] + 4095) & ~4095);
srcBuffer = alignedmemptr[0];
setData(srcBuffer, bufSize_, 0xd0);
}
else
{
err = hipMalloc(&srcBuffer, bufSize_);
CHECK_RESULT(err != hipSuccess, "hipMalloc failed");
err = hipMemset(srcBuffer, 0xd0, bufSize_);
CHECK_RESULT(err != hipSuccess, "hipMemset failed");
}
if (hostMalloc[1])
{
err = hipHostMalloc((void**)&dstBuffer, bufSize_, 0);
CHECK_RESULT(err != hipSuccess, "hipHostMalloc failed");
}
else if (hostRegister[1])
{
memptr[1] = malloc(bufSize_ + 4096);
alignedmemptr[1] = (void*)(((size_t)memptr[1] + 4095) & ~4095);
dstBuffer = alignedmemptr[1];
err = hipHostRegister(dstBuffer, bufSize_, 0);
CHECK_RESULT(err != hipSuccess, "hipHostRegister failed");
}
else if (unpinnedMalloc[1])
{
memptr[1] = malloc(bufSize_ + 4096);
alignedmemptr[1] = (void*)(((size_t)memptr[1] + 4095) & ~4095);
dstBuffer = alignedmemptr[1];
}
else
{
err = hipMalloc(&dstBuffer, bufSize_);
CHECK_RESULT(err != hipSuccess, "hipMalloc failed");
}
CPerfCounter timer;
//warm up
err = hipMemcpy2D(dstBuffer, width, srcBuffer, width, width, width, hipMemcpyDefault);
CHECK_RESULT(err, "hipMemcpy2D failed");
timer.Reset();
timer.Start();
for (unsigned int i = 0; i < numIter; i++)
{
err = hipMemcpy2DAsync(dstBuffer, width, srcBuffer, width, width, width, hipMemcpyDefault, NULL);
CHECK_RESULT(err, "hipMemcpyAsync2D failed");
}
err = hipDeviceSynchronize();
CHECK_RESULT(err, "hipDeviceSynchronize failed");
timer.Stop();
double sec = timer.GetElapsedTime();
// Buffer copy bandwidth in GB/s
double perf = ((double)bufSize_*numIter*(double)(1e-09)) / sec;
const char *strSrc = NULL;
const char *strDst = NULL;
if (hostMalloc[0])
strSrc = "hHM";
else if (hostRegister[0])
strSrc = "hHR";
else if (unpinnedMalloc[0])
strSrc = "unp";
else
strSrc = "hM";
if (hostMalloc[1])
strDst = "hHM";
else if (hostRegister[1])
strDst = "hHR";
else if (unpinnedMalloc[1])
strDst = "unp";
else
strDst = "hM";
// Double results when src and dst are both on device
if ((!hostMalloc[0] && !hostRegister[0] && !unpinnedMalloc[0]) &&
(!hostMalloc[1] && !hostRegister[1] && !unpinnedMalloc[1]))
perf *= 2.0;
// Double results when src and dst are both in sysmem
if ((hostMalloc[0] || hostRegister[0] || unpinnedMalloc[0]) &&
(hostMalloc[1] || hostRegister[1] || unpinnedMalloc[1]))
perf *= 2.0;
char buf[256];
SNPRINTF(buf, sizeof(buf), "HIPPerfBufferCopyRectSpeed[%d]\t(%8d bytes)\ts:%s d:%s\ti:%4d\t(GB/s) perf\t%f",
test, bufSize_, strSrc, strDst, numIter, (float)perf);
printf("%s\n", buf);
//Free src
if (hostMalloc[0])
{
hipHostFree(srcBuffer);
}
else if (hostRegister[0])
{
hipHostUnregister(srcBuffer);
free(memptr[0]);
}
else if (unpinnedMalloc[0])
{
free(memptr[0]);
}
else
{
hipFree(srcBuffer);
}
//Free dst
if (hostMalloc[1])
{
hipHostFree(dstBuffer);
}
else if (hostRegister[1])
{
hipHostUnregister(dstBuffer);
free(memptr[1]);
}
else if (unpinnedMalloc[1])
{
free(memptr[1]);
}
else
{
hipFree(dstBuffer);
}
}
passed();
}
@@ -0,0 +1,287 @@
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <complex>
#include "timer.h"
#include "test_common.h"
/* HIT_START
* BUILD: %t %s ../../src/test_common.cpp timer.cpp EXCLUDE_HIP_PLATFORM nvcc
* TEST: %t
* HIT_END
*/
// Quiet pesky warnings
#ifdef WIN_OS
#define SNPRINTF sprintf_s
#else
#define SNPRINTF snprintf
#endif
#define NUM_SIZES 8
//4KB, 8KB, 64KB, 256KB, 1 MB, 4MB, 16 MB, 16MB+10
static const unsigned int Sizes[NUM_SIZES] = {4096, 8192, 65536, 262144, 1048576, 4194304, 16777216, 16777216+10};
static const unsigned int Iterations[2] = {1, 1000};
#define BUF_TYPES 4
// 16 ways to combine 4 different buffer types
#define NUM_SUBTESTS (BUF_TYPES*BUF_TYPES)
#define CHECK_RESULT(test, msg) \
if ((test)) \
{ \
printf("\n%s\n", msg); \
abort(); \
}
void setData(void *ptr, unsigned int size, char value)
{
char *ptr2 = (char *)ptr;
for (unsigned int i = 0; i < size ; i++)
{
ptr2[i] = value;
}
}
void checkData(void *ptr, unsigned int size, char value)
{
char *ptr2 = (char *)ptr;
for (unsigned int i = 0; i < size; i++)
{
if (ptr2[i] != value)
{
printf("Data validation failed at %d! Got 0x%08x\n", i, ptr2[i]);
printf("Expected 0x%08x\n", value);
CHECK_RESULT(true, "Data validation failed!");
break;
}
}
}
int main(int argc, char* argv[]) {
HipTest::parseStandardArguments(argc, argv, true);
hipError_t err = hipSuccess;
hipDeviceProp_t props = {0};
hipGetDeviceProperties(&props, p_gpuDevice);
CHECK_RESULT(err != hipSuccess, "hipGetDeviceProperties failed" );
printf("Set device to %d : %s\n", p_gpuDevice, props.name);
printf("Legend: unp - unpinned(malloc), hM - hipMalloc(device)\n");
printf(" hHR - hipHostRegister(pinned), hHM - hipHostMalloc(prePinned)\n");
err = hipSetDevice(p_gpuDevice);
CHECK_RESULT(err != hipSuccess, "hipSetDevice failed" );
unsigned int bufSize_;
bool hostMalloc[2] = {false};
bool hostRegister[2] = {false};
bool unpinnedMalloc[2] = {false};
unsigned int numIter;
void *memptr[2] = {NULL};
void *alignedmemptr[2] = {NULL};
void* srcBuffer = NULL;
void* dstBuffer = NULL;
int numTests = (p_tests == -1) ? (NUM_SIZES*NUM_SUBTESTS*2 - 1) : p_tests;
int test = (p_tests == -1) ? 0 : p_tests;
for(;test <= numTests; test++)
{
unsigned int srcTest = (test / NUM_SIZES) % BUF_TYPES;
unsigned int dstTest = (test / (NUM_SIZES*BUF_TYPES)) % BUF_TYPES;
bufSize_ = Sizes[test % NUM_SIZES];
hostMalloc[0] = hostMalloc[1] = false;
hostRegister[0] = hostRegister[1] = false;
unpinnedMalloc[0] = unpinnedMalloc[1] = false;
srcBuffer = dstBuffer = 0;
memptr[0] = memptr[1] = NULL;
alignedmemptr[0] = alignedmemptr[1] = NULL;
if (srcTest == 3)
{
hostRegister[0] = true;
}
else if (srcTest == 2)
{
hostMalloc[0] = true;
}
else if (srcTest == 1)
{
unpinnedMalloc[0] = true;
}
if (dstTest == 1)
{
unpinnedMalloc[1] = true;
}
else if (dstTest == 2)
{
hostMalloc[1] = true;
}
else if (dstTest == 3)
{
hostRegister[1] = true;
}
numIter = Iterations[test / (NUM_SIZES * NUM_SUBTESTS)];
if (hostMalloc[0])
{
err = hipHostMalloc((void**)&srcBuffer, bufSize_, 0);
setData(srcBuffer, bufSize_, 0xd0);
CHECK_RESULT(err != hipSuccess, "hipHostMalloc failed");
}
else if (hostRegister[0])
{
memptr[0] = malloc(bufSize_ + 4096);
alignedmemptr[0] = (void*)(((size_t)memptr[0] + 4095) & ~4095);
srcBuffer = alignedmemptr[0];
setData(srcBuffer, bufSize_, 0xd0);
err = hipHostRegister(srcBuffer, bufSize_, 0);
CHECK_RESULT(err != hipSuccess, "hipHostRegister failed");
}
else if (unpinnedMalloc[0])
{
memptr[0] = malloc(bufSize_ + 4096);
alignedmemptr[0] = (void*)(((size_t)memptr[0] + 4095) & ~4095);
srcBuffer = alignedmemptr[0];
setData(srcBuffer, bufSize_, 0xd0);
}
else
{
err = hipMalloc(&srcBuffer, bufSize_);
CHECK_RESULT(err != hipSuccess, "hipMalloc failed");
err = hipMemset(srcBuffer, 0xd0, bufSize_);
CHECK_RESULT(err != hipSuccess, "hipMemset failed");
}
if (hostMalloc[1])
{
err = hipHostMalloc((void**)&dstBuffer, bufSize_, 0);
CHECK_RESULT(err != hipSuccess, "hipHostMalloc failed");
}
else if (hostRegister[1])
{
memptr[1] = malloc(bufSize_ + 4096);
alignedmemptr[1] = (void*)(((size_t)memptr[1] + 4095) & ~4095);
dstBuffer = alignedmemptr[1];
err = hipHostRegister(dstBuffer, bufSize_, 0);
CHECK_RESULT(err != hipSuccess, "hipHostRegister failed");
}
else if (unpinnedMalloc[1])
{
memptr[1] = malloc(bufSize_ + 4096);
alignedmemptr[1] = (void*)(((size_t)memptr[1] + 4095) & ~4095);
dstBuffer = alignedmemptr[1];
}
else
{
err = hipMalloc(&dstBuffer, bufSize_);
CHECK_RESULT(err != hipSuccess, "hipMalloc failed");
}
CPerfCounter timer;
//warm up
err = hipMemcpy(dstBuffer, srcBuffer, bufSize_, hipMemcpyDefault);
CHECK_RESULT(err, "hipMemcpy failed");
timer.Reset();
timer.Start();
for (unsigned int i = 0; i < numIter; i++)
{
err = hipMemcpyAsync(dstBuffer, srcBuffer, bufSize_, hipMemcpyDefault, NULL);
CHECK_RESULT(err, "hipMemcpyAsync failed");
}
err = hipDeviceSynchronize();
CHECK_RESULT(err, "hipDeviceSynchronize failed");
timer.Stop();
double sec = timer.GetElapsedTime();
// Buffer copy bandwidth in GB/s
double perf = ((double)bufSize_*numIter*(double)(1e-09)) / sec;
const char *strSrc = NULL;
const char *strDst = NULL;
if (hostMalloc[0])
strSrc = "hHM";
else if (hostRegister[0])
strSrc = "hHR";
else if (unpinnedMalloc[0])
strSrc = "unp";
else
strSrc = "hM";
if (hostMalloc[1])
strDst = "hHM";
else if (hostRegister[1])
strDst = "hHR";
else if (unpinnedMalloc[1])
strDst = "unp";
else
strDst = "hM";
// Double results when src and dst are both on device
if ((!hostMalloc[0] && !hostRegister[0] && !unpinnedMalloc[0]) &&
(!hostMalloc[1] && !hostRegister[1] && !unpinnedMalloc[1]))
perf *= 2.0;
// Double results when src and dst are both in sysmem
if ((hostMalloc[0] || hostRegister[0] || unpinnedMalloc[0]) &&
(hostMalloc[1] || hostRegister[1] || unpinnedMalloc[1]))
perf *= 2.0;
char buf[256];
SNPRINTF(buf, sizeof(buf), "HIPPerfBufferCopySpeed[%d]\t(%8d bytes)\ts:%s d:%s\ti:%4d\t(GB/s) perf\t%f",
test, bufSize_, strSrc, strDst, numIter, (float)perf);
printf("%s\n", buf);
// Verification
void* temp = malloc(bufSize_ + 4096);
void* chkBuf = (void*)(((size_t)temp + 4095) & ~4095);
err = hipMemcpy(chkBuf, dstBuffer, bufSize_, hipMemcpyDefault);
CHECK_RESULT(err, "hipMemcpy failed");
checkData(chkBuf, bufSize_, 0xd0);
free(temp);
//Free src
if (hostMalloc[0])
{
hipHostFree(srcBuffer);
}
else if (hostRegister[0])
{
hipHostUnregister(srcBuffer);
free(memptr[0]);
}
else if (unpinnedMalloc[0])
{
free(memptr[0]);
}
else
{
hipFree(srcBuffer);
}
//Free dst
if (hostMalloc[1])
{
hipHostFree(dstBuffer);
}
else if (hostRegister[1])
{
hipHostUnregister(dstBuffer);
free(memptr[1]);
}
else if (unpinnedMalloc[1])
{
free(memptr[1]);
}
else
{
hipFree(dstBuffer);
}
}
passed();
}
@@ -0,0 +1,210 @@
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <complex>
#include "timer.h"
#include "test_common.h"
/* HIT_START
* BUILD: %t %s ../../src/test_common.cpp timer.cpp EXCLUDE_HIP_PLATFORM nvcc
* TEST: %t
* HIT_END
*/
// Quiet pesky warnings
#ifdef WIN_OS
#define SNPRINTF sprintf_s
#else
#define SNPRINTF snprintf
#endif
#define CHAR_BUF_SIZE 512
#define CHECK_RESULT(test, msg) \
if ((test)) \
{ \
printf("\n%s\n", msg); \
abort(); \
}
typedef struct {
unsigned int iterations;
int flushEvery;
} testStruct;
testStruct testList[] =
{
{ 1, -1},
{ 1, -1},
{ 10, 1},
{ 10, -1},
{ 100, 1},
{ 100, 10},
{ 100, -1},
{ 1000, 1},
{ 1000, 10},
{ 1000, 100},
{ 1000, -1},
{ 10000, 1},
{ 10000, 10},
{ 10000, 100},
{ 10000, 1000},
{ 10000, -1},
{ 100000, 1},
{ 100000, 10},
{ 100000, 100},
{ 100000, 1000},
{ 100000, 10000},
{ 100000, -1},
};
unsigned int mapTestList[] = {1, 1, 10, 100, 1000, 10000, 100000};
__global__ void _dispatchSpeed(float *outBuf)
{
int i = (blockIdx.x * blockDim.x + threadIdx.x);
if (i < 0)
outBuf[i] = 0.0f;
};
int main(int argc, char* argv[]) {
HipTest::parseStandardArguments(argc, argv, true);
hipError_t err = hipSuccess;
hipDeviceProp_t props = {0};
hipGetDeviceProperties(&props, p_gpuDevice);
CHECK_RESULT(err != hipSuccess, "hipGetDeviceProperties failed" );
printf("Set device to %d : %s\n", p_gpuDevice, props.name);
unsigned int testListSize = sizeof(testList) / sizeof(testStruct);
int numTests = (p_tests == -1) ? (2*2*testListSize - 1) : p_tests;
int test = (p_tests == -1) ? 0 : p_tests;
float* srcBuffer = NULL;
unsigned int bufSize_ = 64*sizeof(float);
err = hipMalloc(&srcBuffer, bufSize_);
CHECK_RESULT(err != hipSuccess, "hipMalloc failed");
for(;test <= numTests; test++)
{
int openTest = test % testListSize;
bool sleep = false;
bool doWarmup = false;
if ((test / testListSize) % 2)
{
doWarmup = true;
}
if (test >= (testListSize * 2))
{
sleep = true;
}
int threads = (bufSize_ / sizeof(float));
int threads_per_block = 64;
int blocks = (threads/threads_per_block) + (threads % threads_per_block);
hipEvent_t start, stop;
// NULL stream check:
err = hipEventCreate(&start);
err = hipEventCreate(&stop);
CHECK_RESULT(err != hipSuccess, "hipEventCreate failed");
if (doWarmup)
{
hipLaunchKernelGGL(_dispatchSpeed, dim3(blocks), dim3(threads_per_block), 0, hipStream_t(0), srcBuffer);
err = hipDeviceSynchronize();
CHECK_RESULT(err != hipSuccess, "hipDeviceSynchronize failed");
}
CPerfCounter timer;
timer.Reset();
timer.Start();
for (unsigned int i = 0; i < testList[openTest].iterations; i++)
{
hipEventRecord(start, NULL);
hipLaunchKernelGGL(_dispatchSpeed, dim3(blocks), dim3(threads_per_block), 0, hipStream_t(0), srcBuffer);
hipEventRecord(stop, NULL);
if ((testList[openTest].flushEvery > 0) &&
(((i + 1) % testList[openTest].flushEvery) == 0))
{
if (sleep)
{
err = hipDeviceSynchronize();
CHECK_RESULT(err != hipSuccess, "hipDeviceSynchronize failed");
}
else
{
do {
err = hipEventQuery(stop);
} while (err == hipErrorNotReady);
}
}
}
if (sleep)
{
err = hipDeviceSynchronize();
CHECK_RESULT(err != hipSuccess, "hipDeviceSynchronize failed");
}
else
{
do {
err = hipEventQuery(stop);
} while (err == hipErrorNotReady);
}
timer.Stop();
hipEventDestroy(start);
hipEventDestroy(stop);
double sec = timer.GetElapsedTime();
// microseconds per launch
double perf = (1000000.f*sec/testList[openTest].iterations);
const char *waitType;
const char *extraChar;
const char *n;
const char *warmup;
if (sleep)
{
waitType = "sleep";
extraChar = "";
n = "";
}
else
{
waitType = "spin";
n = "n";
extraChar = " ";
}
if (doWarmup)
{
warmup = "warmup";
}
else
{
warmup = "";
}
char buf[256];
if (testList[openTest].flushEvery > 0)
{
SNPRINTF(buf, sizeof(buf), "HIPPerfDispatchSpeed[%3d] %7d dispatches %s%sing every %5d %6s (us/disp) %3f", test, testList[openTest].iterations,
waitType, n, testList[openTest].flushEvery, warmup, (float)perf);
}
else
{
SNPRINTF(buf, sizeof(buf), "HIPPerfDispatchSpeed[%3d] %7d dispatches (%s%s) %6s (us/disp) %3f", test, testList[openTest].iterations,
waitType, extraChar, warmup, (float)perf);
}
printf("%s\n", buf);
}
hipFree(srcBuffer);
passed();
}
@@ -0,0 +1,116 @@
#include "timer.h"
#include <stdlib.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
#pragma comment(lib, "user32")
#endif
#ifdef __linux__
#include <time.h>
#define NANOSECONDS_PER_SEC 1000000000
#endif
CPerfCounter::CPerfCounter() : _clocks(0), _start(0)
{
#ifdef _WIN32
QueryPerformanceFrequency((LARGE_INTEGER *)&_freq);
#endif
#ifdef __linux__
_freq = NANOSECONDS_PER_SEC;
#endif
}
CPerfCounter::~CPerfCounter()
{
// EMPTY!
}
void
CPerfCounter::Start(void)
{
#ifdef _WIN32
if( _start )
{
MessageBox(NULL, "Bad Perf Counter Start", "Error", MB_OK);
exit(0);
}
QueryPerformanceCounter((LARGE_INTEGER *)&_start);
#endif
#ifdef __linux__
struct timespec s;
clock_gettime(CLOCK_MONOTONIC, &s);
_start = (i64)s.tv_sec * NANOSECONDS_PER_SEC + (i64)s.tv_nsec ;
#endif
}
void
CPerfCounter::Stop(void)
{
i64 n;
#ifdef _WIN32
if( !_start )
{
MessageBox(NULL, "Bad Perf Counter Stop", "Error", MB_OK);
exit(0);
}
QueryPerformanceCounter((LARGE_INTEGER *)&n);
#endif
#ifdef __linux__
struct timespec s;
clock_gettime(CLOCK_MONOTONIC, &s);
n = (i64)s.tv_sec * NANOSECONDS_PER_SEC + (i64)s.tv_nsec ;
#endif
n -= _start;
_start = 0;
_clocks += n;
}
void
CPerfCounter::Reset(void)
{
#ifdef _WIN32
if( _start )
{
MessageBox(NULL, "Bad Perf Counter Reset", "Error", MB_OK);
exit(0);
}
#endif
_clocks = 0;
}
double
CPerfCounter::GetElapsedTime(void)
{
#ifdef _WIN32
if( _start ) {
MessageBox(NULL, "Trying to get time while still running.", "Error", MB_OK);
exit(0);
}
#endif
return (double)_clocks / (double)_freq;
}
@@ -0,0 +1,28 @@
#ifndef _TIMER_H_
#define _TIMER_H_
#ifdef _WIN32
typedef __int64 i64 ;
#endif
#ifdef __linux__
typedef long long i64;
#endif
class CPerfCounter {
public:
CPerfCounter();
~CPerfCounter();
void Start(void);
void Stop(void);
void Reset(void);
double GetElapsedTime(void);
private:
i64 _freq;
i64 _clocks;
i64 _start;
};
#endif // _TIMER_H_
@@ -146,7 +146,8 @@ void printDeviceProp(int deviceId) {
cout << setw(w1) << "maxTexture3D.width: " << props.maxTexture3D[0] << endl;
cout << setw(w1) << "maxTexture3D.height: " << props.maxTexture3D[1] << endl;
cout << setw(w1) << "maxTexture3D.depth: " << props.maxTexture3D[2] << endl;
cout << setw(w1) << "isLargeBar: " << props.isLargeBar << endl;
int deviceCnt;
hipGetDeviceCount(&deviceCnt);
cout << setw(w1) << "peers: ";
@@ -11,6 +11,7 @@ set(CMAKE_MODULE_PATH "${HIP_PATH}/cmake" ${CMAKE_MODULE_PATH})
project(12_cmake)
set(HIP_CLANG_NUM_PARALLEL_JOBS 2)
find_package(HIP QUIET)
if(HIP_FOUND)
message(STATUS "Found HIP: " ${HIP_VERSION})
@@ -22,7 +23,8 @@ set(MY_SOURCE_FILES MatrixTranspose.cpp)
set(MY_TARGET_NAME MatrixTranspose)
set(MY_HIPCC_OPTIONS)
set(MY_HCC_OPTIONS)
set(MY_CLANG_OPTIONS)
set(MY_NVCC_OPTIONS)
set_source_files_properties(${MY_SOURCE_FILES} PROPERTIES HIP_SOURCE_PROPERTY_FORMAT 1)
hip_add_executable(${MY_TARGET_NAME} ${MY_SOURCE_FILES} HIPCC_OPTIONS ${MY_HIPCC_OPTIONS} HCC_OPTIONS ${MY_HCC_OPTIONS} NVCC_OPTIONS ${MY_NVCC_OPTIONS})
hip_add_executable(${MY_TARGET_NAME} ${MY_SOURCE_FILES} HIPCC_OPTIONS ${MY_HIPCC_OPTIONS} HCC_OPTIONS ${MY_HCC_OPTIONS} CLANG_OPTIONS ${MY_CLANG_OPTIONS} NVCC_OPTIONS ${MY_NVCC_OPTIONS})
@@ -28,8 +28,8 @@ If your project already modifies ```CMAKE_MODULE_PATH```, you will need to appen
## Using the hip_add_executable macro
FindHIP provides the ```hip_add_executable``` macro that is similar to the ```cuda_add_executable``` macro that is provided by FindCUDA.
The syntax is also similar. The ```hip_add_executable``` macro uses the hipcc wrapper as the compiler.
The macro supports specifying HCC-specific, NVCC-specific compiler options using the ```HCC_OPTIONS``` and ```NVCC_OPTIONS``` keywords.
Common options targeting both compilers can be specificed after the ```HIPCC_OPTIONS``` keyword.
The macro supports specifying HCC-specific, CLANG-specific, NVCC-specific compiler options using the ```HCC_OPTIONS```, ```CLANG_OPTIONS``` and ```NVCC_OPTIONS``` keywords.
Common options targeting both compilers can be specificed after the ```HIPCC_OPTIONS``` keyword.
## How to build and run:
Use the following commands to build and execute the sample
@@ -1,53 +0,0 @@
HIP_PATH?= $(wildcard /opt/rocm/hip)
HIPCC=$(HIP_PATH)/bin/hipcc
HIPPROFILER=/opt/rocm/bin/rocm-profiler
PROFILER_OPT=-A -o MT.atp -e HIP_PROFILE_API=1
HIPPROFILER_POST_CMD=$(HIP_PATH)/bin/hipdemangleatp MT.atp
TARGET=hcc
SOURCES = MatrixTranspose.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECUTABLE=./MatrixTranspose
.PHONY: test
all: $(EXECUTABLE) profile
OPT =-g
CXXFLAGS =$(OPT)
CXX=$(HIPCC)
$(EXECUTABLE): $(OBJECTS)
$(HIPCC) $(OBJECTS) -o $@
profile: $(EXECUTABLE)
$(HIPPROFILER) $(PROFILER_OPT) $(EXECUTABLE)
$(HIPPROFILER_POST_CMD)
# Pass option to control start and stop iterations for profiling - see MatrixTranspose.cpp for implementation:
# Note we start profiler in --startdisabled mode - no timing collected until app enabled it via hipProfilerStart()
profile_trigger: $(EXECUTABLE)
$(HIPPROFILER) $(PROFILER_OPT) --startdisabled $(EXECUTABLE) 3 6
$(HIPPROFILER_POST_CMD)
run: $(EXECUTABLE)
$(EXECUTABLE)
clean:
rm -f $(EXECUTABLE)
rm -f $(OBJECTS)
rm -f $(HIP_PATH)/src/*.o
@@ -1,219 +0,0 @@
/*
Copyright (c) 2015-present Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <iostream>
// hip header file
#include "hip/hip_runtime.h"
#include "hip/hip_profile.h"
#define WIDTH 1024
#define NUM (WIDTH * WIDTH)
#define THREADS_PER_BLOCK_X 4
#define THREADS_PER_BLOCK_Y 4
#define THREADS_PER_BLOCK_Z 1
#define ITERATIONS 10
// Cmdline parms to control start and stop triggers
int startTriggerIteration = -1;
int stopTriggerIteration = -1;
// Device (Kernel) function, it must be void
__global__ void matrixTranspose(float* out, float* in, const int width) {
int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x;
int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y;
out[y * width + x] = in[x * width + y];
}
// CPU implementation of matrix transpose
void matrixTransposeCPUReference(float* output, float* input, const unsigned int width) {
for (unsigned int j = 0; j < width; j++) {
for (unsigned int i = 0; i < width; i++) {
output[i * width + j] = input[j * width + i];
}
}
}
// Use a separate function to demonstrate how to use function name as part of scoped marker:
void runGPU(float* Matrix, float* TransposeMatrix, float* gpuMatrix, float* gpuTransposeMatrix) {
// __func__ is a standard C++ macro which expands to the name of the function, in this case
// "runGPU"
HIP_SCOPED_MARKER(__func__, "MyGroup");
for (int i = 0; i < ITERATIONS; i++) {
if (i == startTriggerIteration) {
hipProfilerStart();
}
if (i == stopTriggerIteration) {
hipProfilerStop();
}
float eventMs = 0.0f;
hipEvent_t start, stop;
hipEventCreate(&start);
hipEventCreate(&stop);
// Record the start event
hipEventRecord(start, NULL);
// Memory transfer from host to device
hipMemcpy(gpuMatrix, Matrix, NUM * sizeof(float), hipMemcpyHostToDevice);
// Record the stop event
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
printf("hipMemcpyHostToDevice time taken = %6.3fms\n", eventMs);
// Record the start event
hipEventRecord(start, NULL);
// Lauching kernel from host
hipLaunchKernelGGL(matrixTranspose,
dim3(WIDTH / THREADS_PER_BLOCK_X, WIDTH / THREADS_PER_BLOCK_Y),
dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y), 0, 0, gpuTransposeMatrix,
gpuMatrix, WIDTH);
// Record the stop event
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
printf("kernel Execution time = %6.3fms\n", eventMs);
// Record the start event
hipEventRecord(start, NULL);
// Memory transfer from device to host
hipMemcpy(TransposeMatrix, gpuTransposeMatrix, NUM * sizeof(float), hipMemcpyDeviceToHost);
// Record the stop event
hipEventRecord(stop, NULL);
hipEventSynchronize(stop);
hipEventElapsedTime(&eventMs, start, stop);
printf("hipMemcpyDeviceToHost time taken = %6.3fms\n", eventMs);
}
};
int main(int argc, char* argv[]) {
if (argc >= 2) {
startTriggerIteration = atoi(argv[1]);
printf("info : will start tracing at iteration:%d\n", startTriggerIteration);
}
if (argc >= 3) {
stopTriggerIteration = atoi(argv[2]);
printf("info : will stop tracing at iteration:%d\n", stopTriggerIteration);
}
float* Matrix;
float* TransposeMatrix;
float* cpuTransposeMatrix;
float* gpuMatrix;
float* gpuTransposeMatrix;
hipDeviceProp_t devProp;
hipGetDeviceProperties(&devProp, 0);
std::cout << "Device name " << devProp.name << std::endl;
{
// Show example of how to create a "scoped marker".
// The scoped marker records the time spent inside the { scope } of the marker - the begin
// timestamp is at the beginning of the code scope, and the end is recorded when the SCOPE
// exits. This can be viewed in CodeXL timeline relative to other GPU and CPU events. This
// marker captures the time spent in setup including host allocation, initialization, and
// device memory allocation.
HIP_SCOPED_MARKER("Setup", "MyGroup");
Matrix = (float*)malloc(NUM * sizeof(float));
TransposeMatrix = (float*)malloc(NUM * sizeof(float));
cpuTransposeMatrix = (float*)malloc(NUM * sizeof(float));
// initialize the input data
for (int i = 0; i < NUM; i++) {
Matrix[i] = (float)i * 10.0f;
}
// allocate the memory on the device side
hipMalloc((void**)&gpuMatrix, NUM * sizeof(float));
hipMalloc((void**)&gpuTransposeMatrix, NUM * sizeof(float));
// FYI, the scoped-marker will be destroyed here when the scope exits, and will record its
// "end" timestamp.
}
runGPU(Matrix, TransposeMatrix, gpuMatrix, gpuTransposeMatrix);
// show how to use explicit begin/end markers:
// We begin the timed region with HIP_BEGIN_MARKER, passing in the markerName and group:
// The region will stop when HIP_END_MARKER is called
// This is another way to mark begin/end - as an alternative to scoped markers.
HIP_BEGIN_MARKER("Check&TearDown", "MyGroup");
int errors = 0;
// CPU MatrixTranspose computation
matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH);
// verify the results
double eps = 1.0E-6;
for (int i = 0; i < NUM; i++) {
if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > eps) {
errors++;
}
}
if (errors != 0) {
printf("FAILED: %d errors\n", errors);
} else {
printf("PASSED!\n");
}
// free the resources on device side
hipFree(gpuMatrix);
hipFree(gpuTransposeMatrix);
// free the resources on host side
free(Matrix);
free(TransposeMatrix);
free(cpuTransposeMatrix);
// This ends the last marker started in this thread, in this case "Check&TearDown"
HIP_END_MARKER();
return errors;
}
@@ -1,47 +0,0 @@
## Using hipEvents to measure performance ###
This tutorial is follow-up of the previous two tutorial where we learn how to write our first hip program, in which we compute Matrix Transpose and in second one, we added feature to measure time taken for memory transfer and kernel execution. In this tutorial, we'll explain how to use the codexl/rocm-profiler for hip timeline tracing. Also, we will augment the source code with additional markers so we can see the high-level application flow alongside the information that CodeXL automatically collects.
## Introduction:
CodeXL and rocm-profiler are the tool used for profiling the application, which is of prominent use in optimizing the application by means of finding the memory bottlenecks and etc.
## Requirement:
[CodeXL Installation](http://gpuopen.com/compute-product/codexl/)
## prerequiste knowledge:
Programmers familiar with CUDA, OpenCL will be able to quickly learn and start coding with the HIP API. In case you are not, don't worry. You choose to start with the best one. We'll be explaining everything assuming you are completely new to gpgpu programming.
## Simple Matrix Transpose
We will be using the Simple Matrix Transpose source code from the previous tutorial as it is.
## Using CodeXL markers for HIP Functions
HIP can generate markers at function being/end which are displayed on the CodeXL timeline view. To do this, you need to install ROCm-Profiler and enable HIP to generate the markers:
1. Install ROCm-Profiler Installing HIP from the rocm pre-built packages, installs the ROCm-Profiler as well. Alternatively, you can build ROCm-Profiler using the instructions given below.
2. Run with profiler enabled to generate ATP file.
(These steps are also captured in the Makefile)
The HIP_PROFILE_API enables display of the HIP APIs on the CodeXL trimeline view.
`/opt/rocm/bin/rocm-profiler -o <outputATPFileName> -A <applicationName> -e HIP_PROFILE_API=1 <applicationArguments>`
##Using HIP_TRACE_API
You can also print the HIP function strings to stderr using HIP_TRACE_API environment variable. This can also be combined with the more detailed debug information provided by the HIP_DB switch. For example:
`HIP_TRACE_API=1 HIP_DB=0x2 ./myHipApp`
Note this trace mode uses colors. "less -r" can handle raw control characters and will display the debug output in proper colors.
## More Info:
- [HIP FAQ](https://github.com/ROCm-Developer-Tools/HIP/blob/master/docs/markdown/hip_faq.md)
- [HIP Kernel Language](https://github.com/ROCm-Developer-Tools/HIP/blob/master/docs/markdown/hip_kernel_language.md)
- [HIP Runtime API (Doxygen)](http://rocm-developer-tools.github.io/HIP)
- [HIP Porting Guide](https://github.com/ROCm-Developer-Tools/HIP/blob/master/docs/markdown/hip_porting_guide.md)
- [HIP Terminology](https://github.com/ROCm-Developer-Tools/HIP/blob/master/docs/markdown/hip_terms.md) (including Rosetta Stone of GPU computing terms across CUDA/HIP/HC/AMP/OpenL)
- [HIPIFY](https://github.com/ROCm-Developer-Tools/HIP/blob/master/hipify-clang/README.md)
- [Developer/CONTRIBUTING Info](https://github.com/ROCm-Developer-Tools/HIP/blob/master/CONTRIBUTING.md)
- [Release Notes](https://github.com/ROCm-Developer-Tools/HIP/blob/master/RELEASE.md)