diff --git a/hipamd/tests/src/runtimeApi/device/hipSetGetDevice.cpp b/hipamd/tests/src/runtimeApi/device/hipSetGetDevice.cpp index b74a1bfbe7..74bf910b72 100644 --- a/hipamd/tests/src/runtimeApi/device/hipSetGetDevice.cpp +++ b/hipamd/tests/src/runtimeApi/device/hipSetGetDevice.cpp @@ -25,26 +25,29 @@ */ /* HIT_START - * BUILD: %t %s ../../test_common.cpp NVCC_OPTIONS -std=c++11 EXCLUDE_HIP_PLATFORM nvidia - * TEST_NAMED: %t hipSetGetDevice-invalidDevice - * TEST_NAMED: %t hipSetGetDevice-allValidDevice - * TEST_NAMED: %t hipSetGetDevice-validDev1 --computeDevCnt 1 - * TEST_NAMED: %t hipSetGetDevice-validDev2 --computeDevCnt 2 - * TEST_NAMED: %t hipSetGetDevice-validDev3 --computeDevCnt 3 - * TEST_NAMED: %t hipSetGetDevice-validDev4 --computeDevCnt 4 - * TEST_NAMED: %t hipSetGetDevice-validDev5 --computeDevCnt 5 - * TEST_NAMED: %t hipSetGetDevice-validDev6 --computeDevCnt 6 - * TEST_NAMED: %t hipSetGetDevice-validDev7 --computeDevCnt 7 - * TEST_NAMED: %t hipSetGetDevice-validDev8 --computeDevCnt 8 + * BUILD: %t %s ../../test_common.cpp NVCC_OPTIONS -std=c++11 + * TEST_NAMED: %t hipSetGetDevice-invalidDevice --tests 1 + * TEST_NAMED: %t hipSetGetDevice-allValidDevice --tests 2 + * TEST_NAMED: %t hipSetGetDevice-validDev1 --computeDevCnt 1 --tests 4 + * TEST_NAMED: %t hipSetGetDevice-validDev2 --computeDevCnt 2 --tests 4 + * TEST_NAMED: %t hipSetGetDevice-validDev3 --computeDevCnt 3 --tests 4 + * TEST_NAMED: %t hipSetGetDevice-validDev4 --computeDevCnt 4 --tests 4 + * TEST_NAMED: %t hipSetGetDevice-validDev5 --computeDevCnt 5 --tests 4 + * TEST_NAMED: %t hipSetGetDevice-validDev6 --computeDevCnt 6 --tests 4 + * TEST_NAMED: %t hipSetGetDevice-validDev7 --computeDevCnt 7 --tests 4 + * TEST_NAMED: %t hipSetGetDevice-validDev8 --computeDevCnt 8 --tests 4 + * TEST_NAMED: %t hipSetGetDevice-SetbothEnvVar --tests 5 * HIT_END */ -#include "test_common.h" #include #include +#include "test_common.h" +int sequence_num = 0; void getDeviceCount(int *numDevices) { int fd[2], val = 0; +#ifdef __unix__ pipe(fd); pid_t childPid; @@ -60,8 +63,12 @@ void getDeviceCount(int *numDevices) { int devCnt = 0; close(fd[0]); +#ifdef __HIP_PLATFORM_NVCC__ + unsetenv("CUDA_VISIBLE_DEVICES"); +#else unsetenv("ROCR_VISIBLE_DEVICES"); unsetenv("HIP_VISIBLE_DEVICES"); +#endif hipGetDeviceCount(&devCnt); @@ -72,6 +79,9 @@ void getDeviceCount(int *numDevices) { } else { failed("fork() failed. Exiting the test\n"); } +#else + printf("skipping testcase for non-unix systems\n"); +#endif } #define MAX_SIZE 30 @@ -84,6 +94,7 @@ bool testInvalidDevice(int numDevices, bool useRocrEnv, int deviceNumber) { int setDeviceErrorCheck = 0; int getDeviceErrorCheck = 0; int getDeviceCountErrorCheck = 0; +#ifdef __unix__ int fd[2]; pipe(fd); @@ -95,39 +106,34 @@ bool testInvalidDevice(int numDevices, bool useRocrEnv, int deviceNumber) { if (cPid == 0) { // child hipError_t err; - +#ifdef __HIP_PLATFORM_NVCC__ + setenv("CUDA_VISIBLE_DEVICES", visibleDeviceString, 1); +#else if (true == useRocrEnv) { setenv("ROCR_VISIBLE_DEVICES", visibleDeviceString, 1); } else { setenv("HIP_VISIBLE_DEVICES", visibleDeviceString, 1); } - +#endif err = hipGetDeviceCount(&tempCount); - if (err == hipErrorNoDevice) { + if (err != hipSuccess) { getDeviceCountErrorCheck = 1; - } else { - printf("hipGetDeviceCount returns wrong value: %u\n", hipError_t(err)); } - for (int i = 0; i < numDevices; i++) { err = hipSetDevice(i); - if (err == hipErrorInvalidDevice) { + if (err != hipSuccess) { setDeviceErrorCheck+= 1; - } else { - printf("hipSetDevice returns wrong value: %u\n", hipError_t(err)); } err = hipGetDevice(&device); - if (err == hipErrorNoDevice) { + if (err != hipSuccess) { getDeviceErrorCheck+= 1; - } else { - printf("hipGetDevice returns wrong value: %u\n", hipError_t(err)); } } if ((getDeviceCountErrorCheck == 1) && (setDeviceErrorCheck == numDevices) && (getDeviceErrorCheck == numDevices)) { - testResult = true; + testResult = true; } else { printf("Test failed for invalid device\n"); @@ -149,6 +155,9 @@ bool testInvalidDevice(int numDevices, bool useRocrEnv, int deviceNumber) { printf("fork() failed\n"); testResult = false; } +#else + printf("skipping testcase for non-unix systems\n"); +#endif return testResult; } @@ -160,18 +169,18 @@ int parseExtraArguments(int argc, char* argv[]) { if (!strcmp(arg, " ")) { // skip NULL args. } else if (!strcmp(arg, "--computeDevCnt")) { - if (++i >= argc || !HipTest::parseInt(argv[i], &deviceListLength)) { - failed("Bad deviceListLength argument"); - } + if (++i >= argc || !HipTest::parseInt(argv[i], &deviceListLength)) { + failed("Bad deviceListLength argument"); + } } else { - failed("Bad argument"); + failed("Bad argument"); } } return i; } bool testValidDevices(int numDevices, bool useRocrEnv, int *deviceList, - int deviceListLength) { + int deviceListLength) { bool testResult = true; int tempCount = 0; int device; @@ -180,9 +189,10 @@ bool testValidDevices(int numDevices, bool useRocrEnv, int *deviceList, int getDeviceCountErrorCheck = 0; int *deviceListPtr = deviceList; char visibleDeviceString[MAX_SIZE] = {}; +#ifdef __unix__ if ((NULL == deviceList) || ((deviceListLength < 1) || - deviceListLength > numDevices)) { + deviceListLength > numDevices)) { printf("Invalid argument for number of devices. Skipping current test\n"); return testResult; } @@ -193,7 +203,7 @@ bool testValidDevices(int numDevices, bool useRocrEnv, int *deviceList, return testResult; } snprintf(visibleDeviceString + strlen(visibleDeviceString), MAX_SIZE, "%d,", - *deviceListPtr++); + *deviceListPtr++); } visibleDeviceString[strlen(visibleDeviceString)-1] = 0; @@ -205,14 +215,19 @@ bool testValidDevices(int numDevices, bool useRocrEnv, int *deviceList, cPid = fork(); if (cPid == 0) { +#ifdef __HIP_PLATFORM_NVCC__ + unsetenv("CUDA_VISIBLE_DEVICES"); + setenv("CUDA_VISIBLE_DEVICES", visibleDeviceString, 1); +#else unsetenv("ROCR_VISIBLE_DEVICES"); unsetenv("HIP_VISIBLE_DEVICES"); - if (true == useRocrEnv) { setenv("ROCR_VISIBLE_DEVICES", visibleDeviceString, 1); } else { setenv("HIP_VISIBLE_DEVICES", visibleDeviceString, 1); } +#endif + hipError_t err; err = hipGetDeviceCount(&tempCount); @@ -225,18 +240,18 @@ bool testValidDevices(int numDevices, bool useRocrEnv, int *deviceList, for (int i = 0; i < numDevices; i++) { err = hipSetDevice(i); - if (err == hipErrorInvalidDevice) { + if (err != hipSuccess) { setDeviceErrorCheck+= 1; } err = hipGetDevice(&device); - if (err == hipErrorNoDevice) { + if (err != hipSuccess) { getDeviceErrorCheck+= 1; } } if ((getDeviceCountErrorCheck == 1) && (setDeviceErrorCheck == - (numDevices-deviceListLength)) && (getDeviceErrorCheck == 0)) { + (numDevices-deviceListLength)) && (getDeviceErrorCheck == 0)) { testResult = true; } else { @@ -259,6 +274,9 @@ bool testValidDevices(int numDevices, bool useRocrEnv, int *deviceList, printf("fork() failed\n"); testResult = false; } +#else + printf("skipping testcase for non unix system \n)"; +#endif return testResult; } @@ -284,6 +302,250 @@ bool testValidDevicesBasic() { return testResult; } + +void Initialize(int *deviceList, int numDevices, int count, + char min_visibleDeviceString[], char max_visibleDeviceString[]) { + int *deviceListPtr = deviceList; + for (int i =0; i < count; i++) { + if (i == count-1) { + snprintf(min_visibleDeviceString + strlen(min_visibleDeviceString), + MAX_SIZE, "%d", *deviceListPtr++); + } else { + snprintf(min_visibleDeviceString + strlen(min_visibleDeviceString), + MAX_SIZE, "%d,", *deviceListPtr++); + } + } + for (int i =0; i < numDevices; i++) { + if (i == numDevices-1) { + snprintf(max_visibleDeviceString + strlen(max_visibleDeviceString), + MAX_SIZE, "%d", i); + } else { + snprintf(max_visibleDeviceString + strlen(max_visibleDeviceString), + MAX_SIZE, "%d,", i); + } + } +} + +bool testMaxRvdMinHvd(int numDevices, int *deviceList, int count) { + bool testResult = true; + int device; +#ifdef __unix__ + int validateCount = 0; + char min_visibleDeviceString[MAX_SIZE] = {0}; + char max_visibleDeviceString[MAX_SIZE] = {0}; + int fd[2]; + pipe(fd); + pid_t cPid; + cPid = fork(); + if (cPid == 0) { // child + Initialize(deviceList, numDevices, + count, min_visibleDeviceString, max_visibleDeviceString); + unsetenv("ROCR_VISIBLE_DEVICES"); + unsetenv("HIP_VISIBLE_DEVICES"); + setenv("ROCR_VISIBLE_DEVICES", max_visibleDeviceString, 1); + setenv("HIP_VISIBLE_DEVICES", min_visibleDeviceString, 1); + HIPCHECK(hipGetDeviceCount(&numDevices)); + for (int i = 0; i < numDevices; i++) { + HIPCHECK(hipSetDevice(i)); + HIPCHECK(hipGetDevice(&device)); + if (device == i) { + validateCount+= 1; + } + } + if (count != validateCount) { + testResult = false; + } + } else if (cPid > 0) { + close(fd[1]); + read(fd[0], &testResult, sizeof(testResult)); + close(fd[0]); + wait(NULL); + } else { + printf("fork() failed\n"); + testResult = false; + } +#else + printf("skipping testcase for non unix system \n)"; +#endif + return testResult; +} + +bool testRvdCvd(int numDevices, int *deviceList, int count) { + bool testResult = true; + int device; +#ifdef __unix__ + int validateCount = 0; + char min_visibleDeviceString[MAX_SIZE] = {0}; + char max_visibleDeviceString[MAX_SIZE] = {0}; + int fd[2]; + pipe(fd); + pid_t cPid; + cPid = fork(); + if (cPid == 0) { // child + Initialize(deviceList, numDevices, count, + min_visibleDeviceString, max_visibleDeviceString); + unsetenv("ROCR_VISIBLE_DEVICES"); + unsetenv("HIP_VISIBLE_DEVICES"); + setenv("ROCR_VISIBLE_DEVICES", max_visibleDeviceString, 1); + setenv("CUDA_VISIBLE_DEVICES", min_visibleDeviceString, 1); + HIPCHECK(hipGetDeviceCount(&numDevices)); + for (int i = 0; i < numDevices; i++) { + HIPCHECK(hipSetDevice(i)); + HIPCHECK(hipGetDevice(&device)); + if (device == i) { + validateCount+= 1; + } + } + if (count != validateCount) { + testResult = false; + } + } else if (cPid > 0) { + close(fd[1]); + read(fd[0], &testResult, sizeof(testResult)); + close(fd[0]); + wait(NULL); + } else { + printf("fork() failed\n"); + testResult = false; + } +#else + printf("skipping testcase for non unix system \n)"; +#endif + return testResult; +} + +bool testMinRvdMaxHvd(int numDevices, int *deviceList, int count) { + bool testResult = true; + int device; +#ifdef __unix__ + int validateCount = 0; + char min_visibleDeviceString[MAX_SIZE] = {0}; + char max_visibleDeviceString[MAX_SIZE] = {0}; + int fd[2]; + pipe(fd); + pid_t cPid; + cPid = fork(); + if (cPid == 0) { // child + Initialize(deviceList, numDevices, count, + min_visibleDeviceString, max_visibleDeviceString); + unsetenv("ROCR_VISIBLE_DEVICES"); + unsetenv("HIP_VISIBLE_DEVICES"); + setenv("ROCR_VISIBLE_DEVICES", min_visibleDeviceString, 1); + setenv("HIP_VISIBLE_DEVICES", max_visibleDeviceString, 1); + HIPCHECK(hipGetDeviceCount(&numDevices)); + for (int i = 0; i < numDevices; i++) { + HIPCHECK(hipSetDevice(i)); + HIPCHECK(hipGetDevice(&device)); + if (device == i) { + validateCount+= 1; + } + } + if (count != validateCount) { + testResult = false; + } + close(fd[0]); + write(fd[1], &testResult, sizeof(testResult)); + close(fd[1]); + exit(0); + } else if (cPid > 0) { + close(fd[1]); + read(fd[0], &testResult, sizeof(testResult)); + close(fd[0]); + wait(NULL); + } else { + printf("fork() failed\n"); + testResult = false; + } +#else + printf("skipping testcase for non unix system \n)"; +#endif + return testResult; +} + + +bool testDeviceListSequence(int numDevices, bool useRocrEnv, + int *deviceList, int count) { + bool testResult = true; +#ifdef __unix__ + int validateCount = 0; + int device; + char visibleDeviceString[MAX_SIZE] = {0}; + int tempCount = 0; + int *deviceListPtr = deviceList; + int fd[2]; + if (NULL == deviceList) { + printf("Invalid argument for number of devices. Skipping current test\n"); + return testResult; + } + + pipe(fd); + pid_t cPid; + cPid = fork(); + for (int i =0; i < numDevices; i++) { + if (i == numDevices-1) { + snprintf(visibleDeviceString + strlen(visibleDeviceString), + MAX_SIZE, "%d", *deviceListPtr++); + } else { + snprintf(visibleDeviceString + strlen(visibleDeviceString), + MAX_SIZE, "%d,", *deviceListPtr++); + } + } + if (cPid == 0) { // child + hipError_t err; +#ifdef __HIP_PLATFORM_NVCC__ + unsetenv("CUDA_VISIBLE_DEVICES"); + setenv("CUDA_VISIBLE_DEVICES", visibleDeviceString, 1); +#else + unsetenv("ROCR_VISIBLE_DEVICES"); + unsetenv("HIP_VISIBLE_DEVICES"); + if (true == useRocrEnv) { + setenv("ROCR_VISIBLE_DEVICES", visibleDeviceString, 1); + } else { + setenv("HIP_VISIBLE_DEVICES", visibleDeviceString, 1); + } +#endif + err = hipGetDeviceCount(&tempCount); + if (err == hipSuccess) { + for (int i = 0; i < numDevices; i++) { + err = hipSetDevice(i); + if (err == hipSuccess) { + err = hipGetDevice(&device); + if (err == hipSuccess && device == i) { + validateCount += 1; + } + } + } + if (count != tempCount || tempCount != validateCount) { + testResult = false; + } else { + testResult = true; + } + + } else { +#ifdef __HIP_PLATFORM_NVCC__ + testResult = true; +#endif + } + close(fd[0]); + write(fd[1], &testResult, sizeof(testResult)); + close(fd[1]); + exit(0); + } else if (cPid > 0) { // parent + close(fd[1]); + read(fd[0], &testResult, sizeof(testResult)); + close(fd[0]); + wait(NULL); + } else { + printf("fork() failed\n"); + testResult = false; + } +#else + printf("skipping testcase for non unix system \n)"; +#endif + return testResult; +} + + int main(int argc, char* argv[]) { bool testResult = true; int numDevices = 0; @@ -302,18 +564,20 @@ int main(int argc, char* argv[]) { extraArgs = HipTest::parseStandardArguments(argc, argv, false); parseExtraArguments(extraArgs, argv); - - if (extraArgs == 1) { + if (p_tests == 1) { printf("\nRunning test for invalid compute device\n"); +#ifndef __HIP_PLATFORM_NVCC__ // Test setting -1 to ROCR_VISIBLE_DEVICES testResult &= testInvalidDevice(numDevices, true, -1); - // Test setting -1 to HIP_VISIBLE_DEVICES - testResult &= testInvalidDevice(numDevices, false, -1); + // Test setting invalid device to ROCR_VISIBLE_DEVICES testResult &= testInvalidDevice(numDevices, true, numDevices); +#endif + // Test setting -1 to HIP_VISIBLE_DEVICES + testResult &= testInvalidDevice(numDevices, false, -1); // Test setting invalide device to HIP_VISIBLE_DEVICES testResult &= testInvalidDevice(numDevices, false, numDevices); - + } else if (p_tests == 2) { // Test for all available devices printf("\nRunning test for all available compute devices\n"); @@ -321,35 +585,92 @@ int main(int argc, char* argv[]) { deviceList[i] = i; } +#ifndef __HIP_PLATFORM_NVCC__ testResult &= testValidDevices(numDevices, true, deviceList, numDevices); +#endif testResult &= testValidDevices(numDevices, false, deviceList, numDevices); - } - - // Assigning values to deviceList in reverse order - - for (int i=0; i < deviceListLength; i++) { - deviceList[i] = deviceListLength-1-i; - } - - // Test for subset of available gpus - if (extraArgs == 3) { + } else if (p_tests == 3) { + printf("Running test for various invalid and valid sequences\n"); + int count; + if (numDevices >= 2) + count = 2; + else + count = numDevices; + // Assigning values to deviceList in reverse order + for (int i=0; i < numDevices; i++) { + if (i%2 == 0) { + deviceList[i] = -1; + } else { + deviceList[i] = i; + } + } +#ifndef __HIP_PLATFORM_NVCC__ + testResult = testDeviceListSequence(numDevices, true, deviceList, count); +#endif + testResult = testDeviceListSequence(numDevices, false, deviceList, count); + count = 1; + for (int i=0; i < numDevices; i++) { + if (i/2 == 0) { + deviceList[i] = 0; + } else { + deviceList[i] = i; + } + } +#ifndef __HIP_PLATFORM_NVCC__ + testResult = testDeviceListSequence(numDevices, true, deviceList, count); +#endif + testResult = testDeviceListSequence(numDevices, false, deviceList, count); + if (numDevices == 1) { + deviceList[0] = 0; + } else { + for (int i=0; i < numDevices; i++) { + deviceList[i] = 1; + } + } +#ifndef __HIP_PLATFORM_NVCC__ + testResult &= testDeviceListSequence(numDevices, true, deviceList, count); +#endif + testResult &= testDeviceListSequence(numDevices, false, deviceList, count); + } else if (p_tests == 4) { + // Test for subset of available gpus + for (int i=0; i < deviceListLength; i++) { + deviceList[i] = deviceListLength-1-i; + } printf("\nRunning test for %d compute devices\n", deviceListLength); +#ifndef __HIP_PLATFORM_NVCC__ testResult &= testValidDevices(numDevices, true, deviceList, - deviceListLength); + deviceListLength); +#endif testResult &= testValidDevices(numDevices, false, deviceList, - deviceListLength); + deviceListLength); + } else if (p_tests == 5) { +#ifndef __HIP_PLATFORM_NVCC__ + int count = 0; + if (numDevices == 1) { + deviceList[0] = 0; + count = 1; + } else { + for (int i=0; i < numDevices; i++) { + if (i%2 == 0) { + deviceList[count] = i; + count++; + } + } + } + testResult &= testMinRvdMaxHvd(numDevices, deviceList, count); + testResult &= testMaxRvdMinHvd(numDevices, deviceList, count); + testResult &= testRvdCvd(numDevices, deviceList, count); +#endif + } else { + failed("Didnt receive any valid option. Try options 1 to 5\n"); } - #else - printf("Running basic test on Windows\n"); testResult &= testValidDevicesBasic(); #endif - if (testResult == true) { passed(); - } else { failed("One or more tests failed\n"); }