Skip to content

Commit

Permalink
Code refactoring due to readability sake
Browse files Browse the repository at this point in the history
  • Loading branch information
shajder committed Aug 30, 2024
1 parent b48b8c7 commit 222fd8e
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(MODULE_NAME CL_KHR_SEMAPHORE)
set(${MODULE_NAME}_SOURCES
main.cpp
test_semaphores.cpp
test_semaphores_queries.cpp
test_semaphores_negative_release_retain.cpp
test_semaphores_negative_getinfo.cpp
test_semaphores_negative_wait.cpp
Expand Down
260 changes: 0 additions & 260 deletions test_conformance/extensions/cl_khr_semaphore/test_semaphores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,12 @@
// limitations under the License.
//


#include "harness/typeWrappers.h"
#include "harness/errorHelpers.h"
#include <system_error>
#include <thread>
#include <chrono>

#include "semaphore_base.h"

#define FLUSH_DELAY_S 5

#define SEMAPHORE_PARAM_TEST(param_name, param_type, expected) \
do \
{ \
param_type value; \
size_t size; \
cl_int error = clGetSemaphoreInfoKHR(semaphore, param_name, \
sizeof(value), &value, &size); \
test_error(error, "Unable to get " #param_name " from semaphore"); \
if (value != expected) \
{ \
test_fail("ERROR: Parameter %s did not validate! (expected %d, " \
"got %d)\n", \
#param_name, expected, value); \
} \
if (size != sizeof(value)) \
{ \
test_fail( \
"ERROR: Returned size of parameter %s does not validate! " \
"(expected %d, got %d)\n", \
#param_name, (int)sizeof(value), (int)size); \
} \
} while (false)

#define SEMAPHORE_PARAM_TEST_ARRAY(param_name, param_type, num_params, \
expected) \
do \
{ \
param_type value[num_params]; \
size_t size; \
cl_int error = clGetSemaphoreInfoKHR(semaphore, param_name, \
sizeof(value), &value, &size); \
test_error(error, "Unable to get " #param_name " from semaphore"); \
if (size != sizeof(value)) \
{ \
test_fail( \
"ERROR: Returned size of parameter %s does not validate! " \
"(expected %d, got %d)\n", \
#param_name, (int)sizeof(value), (int)size); \
} \
if (memcmp(value, expected, size) != 0) \
{ \
test_fail("ERROR: Parameter %s did not validate!\n", #param_name); \
} \
} while (false)

namespace {

const char* source = "__kernel void empty() {}";
Expand Down Expand Up @@ -514,185 +464,6 @@ struct SemaphoreMultiWait : public SemaphoreTestBase
clSemaphoreWrapper semaphore_second = nullptr;
};

struct SemaphoreWithDeviceListQueries : public SemaphoreTestBase
{
SemaphoreWithDeviceListQueries(cl_device_id device, cl_context context,
cl_command_queue queue)
: SemaphoreTestBase(device, context, queue)
{}

cl_int Run() override
{
cl_int err = CL_SUCCESS;

// Query binary semaphore created with
// CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR

// Create binary semaphore
cl_semaphore_properties_khr sema_props[] = {
static_cast<cl_semaphore_properties_khr>(CL_SEMAPHORE_TYPE_KHR),
static_cast<cl_semaphore_properties_khr>(
CL_SEMAPHORE_TYPE_BINARY_KHR),
static_cast<cl_semaphore_properties_khr>(
CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR),
(cl_semaphore_properties_khr)device,
CL_SEMAPHORE_DEVICE_HANDLE_LIST_END_KHR,
0
};
semaphore =
clCreateSemaphoreWithPropertiesKHR(context, sema_props, &err);
test_error(err, "Could not create semaphore");

// Confirm that querying CL_SEMAPHORE_TYPE_KHR returns
// CL_SEMAPHORE_TYPE_BINARY_KHR
SEMAPHORE_PARAM_TEST(CL_SEMAPHORE_TYPE_KHR, cl_semaphore_type_khr,
CL_SEMAPHORE_TYPE_BINARY_KHR);

// Confirm that querying CL_SEMAPHORE_CONTEXT_KHR returns the right
// context
SEMAPHORE_PARAM_TEST(CL_SEMAPHORE_CONTEXT_KHR, cl_context, context);

// Confirm that querying CL_SEMAPHORE_REFERENCE_COUNT_KHR returns
// the right value
SEMAPHORE_PARAM_TEST(CL_SEMAPHORE_REFERENCE_COUNT_KHR, cl_uint, 1);

err = clRetainSemaphoreKHR(semaphore);
test_error(err, "Could not retain semaphore");
SEMAPHORE_PARAM_TEST(CL_SEMAPHORE_REFERENCE_COUNT_KHR, cl_uint, 2);

err = clReleaseSemaphoreKHR(semaphore);
test_error(err, "Could not release semaphore");
SEMAPHORE_PARAM_TEST(CL_SEMAPHORE_REFERENCE_COUNT_KHR, cl_uint, 1);

// Confirm that querying CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR returns
// the same device id the semaphore was created with
SEMAPHORE_PARAM_TEST(CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR, cl_device_id,
device);

// Confirm that querying CL_SEMAPHORE_PROPERTIES_KHR returns the
// same properties the semaphore was created with
SEMAPHORE_PARAM_TEST_ARRAY(CL_SEMAPHORE_PROPERTIES_KHR,
cl_semaphore_properties_khr, 6, sema_props);

// Confirm that querying CL_SEMAPHORE_PAYLOAD_KHR returns the
// unsignaled state
SEMAPHORE_PARAM_TEST(CL_SEMAPHORE_PAYLOAD_KHR, cl_semaphore_payload_khr,
0);

return TEST_PASS;
}
};

struct SemaphoreNoDeviceListQueries : public SemaphoreTestBase
{
SemaphoreNoDeviceListQueries(cl_device_id device, cl_context context,
cl_command_queue queue)
: SemaphoreTestBase(device, context, queue)
{}

cl_int Run() override
{
cl_int err = CL_SUCCESS;

// Query binary semaphore created without
// CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR

// Create binary semaphore
cl_semaphore_properties_khr sema_props[] = {
static_cast<cl_semaphore_properties_khr>(CL_SEMAPHORE_TYPE_KHR),
static_cast<cl_semaphore_properties_khr>(
CL_SEMAPHORE_TYPE_BINARY_KHR),
0
};
semaphore =
clCreateSemaphoreWithPropertiesKHR(context, sema_props, &err);
test_error(err, "Could not create semaphore");

// Confirm that querying CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR returns
// device id the semaphore was created with
SEMAPHORE_PARAM_TEST(CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR, cl_device_id,
device);

return TEST_PASS;
}
};

struct SemaphoreMultiDeviceContextQueries : public SemaphoreTestBase
{
SemaphoreMultiDeviceContextQueries(cl_device_id device, cl_context context,
cl_command_queue queue)
: SemaphoreTestBase(device, context, queue)
{}

cl_int Run() override
{
// partition device and create new context if possible
cl_uint maxComputeUnits = 0;
cl_int err =
clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS,
sizeof(maxComputeUnits), &maxComputeUnits, NULL);
test_error(err, "Unable to get maximal number of compute units");

cl_device_partition_property partitionProp[] = {
CL_DEVICE_PARTITION_EQUALLY, maxComputeUnits / 2, 0
};

cl_uint deviceCount = 0;
// how many sub-devices can we create?
err =
clCreateSubDevices(device, partitionProp, 0, nullptr, &deviceCount);
if (err != CL_SUCCESS)
{
log_info("Can't partition device, test not supported\n");
return TEST_SKIPPED_ITSELF;
}

if (deviceCount < 2)
test_error_ret(
CL_INVALID_VALUE,
"Multi context test for CL_INVALID_PROPERTY not supported",
TEST_SKIPPED_ITSELF);

// get the list of subDevices
SubDevicesScopeGuarded scope_guard(deviceCount);
err = clCreateSubDevices(device, partitionProp, deviceCount,
scope_guard.sub_devices.data(), &deviceCount);
if (err != CL_SUCCESS)
{
log_info("Can't partition device, test not supported\n");
return TEST_SKIPPED_ITSELF;
}

/* Create a multi device context */
clContextWrapper multi_device_context = clCreateContext(
NULL, (cl_uint)deviceCount, scope_guard.sub_devices.data(), nullptr,
nullptr, &err);
test_error_ret(err, "Unable to create testing context", CL_SUCCESS);

cl_semaphore_properties_khr sema_props[] = {
(cl_semaphore_properties_khr)CL_SEMAPHORE_TYPE_KHR,
(cl_semaphore_properties_khr)CL_SEMAPHORE_TYPE_BINARY_KHR,
static_cast<cl_semaphore_properties_khr>(
CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR),
(cl_semaphore_properties_khr)scope_guard.sub_devices[0],
CL_SEMAPHORE_DEVICE_HANDLE_LIST_END_KHR,
0
};

// Try to create semaphore with multi device context
semaphore = clCreateSemaphoreWithPropertiesKHR(multi_device_context,
sema_props, &err);
test_error(err, "Unable to create semaphore with properties");

// Confirm that querying CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR returns
// the same device id the semaphore was created with
SEMAPHORE_PARAM_TEST(CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR, cl_device_id,
scope_guard.sub_devices[0]);

return TEST_PASS;
}
};

struct SemaphoreImportExportFD : public SemaphoreTestBase
{
SemaphoreImportExportFD(cl_device_id device, cl_context context,
Expand Down Expand Up @@ -839,37 +610,6 @@ int test_semaphores_multi_wait(cl_device_id deviceID, cl_context context,
return MakeAndRunTest<SemaphoreMultiWait>(deviceID, context, defaultQueue);
}

// Confirm the semaphore with device list can be successfully queried
int test_semaphores_device_list_queries(cl_device_id deviceID,
cl_context context,
cl_command_queue defaultQueue,
int num_elements)
{
return MakeAndRunTest<SemaphoreWithDeviceListQueries>(deviceID, context,
defaultQueue);
}

// Confirm the semaphore without device list can be successfully queried
int test_semaphores_no_device_list_queries(cl_device_id deviceID,
cl_context context,
cl_command_queue defaultQueue,
int num_elements)
{
return MakeAndRunTest<SemaphoreNoDeviceListQueries>(deviceID, context,
defaultQueue);
}

// Confirm the semaphore created with multi-device context can be successfully
// queried
int test_semaphores_multi_device_context_queries(cl_device_id deviceID,
cl_context context,
cl_command_queue defaultQueue,
int num_elements)
{
return MakeAndRunTest<SemaphoreMultiDeviceContextQueries>(deviceID, context,
defaultQueue);
}

// Test it is possible to export a semaphore to a sync fd and import the same
// sync fd to a new semaphore
int test_semaphores_import_export_fd(cl_device_id deviceID, cl_context context,
Expand Down
Loading

0 comments on commit 222fd8e

Please sign in to comment.