Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ static bool checkPlatformIndex(
fprintf(stderr, "Error: No OpenCL platforms found.\n");
return false;
}
if (platformIndex < 0) {
fprintf(stderr, "Error: Invalid platform index %d specified\n",
platformIndex);
return false;
}
if (platformIndex >= (int)platforms.size()) {
fprintf(stderr, "Error: Invalid platform index %d specified (max %d)\n",
platformIndex,
Expand All @@ -106,3 +111,67 @@ static bool checkPlatformIndex(
}
return true;
}

static bool checkDeviceIndex(
const std::vector<cl::Device>& devices,
int deviceIndex)
Comment thread
bashbaug marked this conversation as resolved.
{
if (devices.size() == 0) {
fprintf(stderr, "Error: No OpenCL devices found.\n");
return false;
}
if (deviceIndex < 0) {
fprintf(stderr, "Error: Invalid device index %d specified\n",
deviceIndex);
return false;
}
if (deviceIndex >= (int)devices.size()) {
fprintf(stderr, "Error: Invalid device index %d specified (max %d)\n",
deviceIndex,
(int)(devices.size() - 1) );
return false;
}
return true;
}

static bool setupDevice(
cl::Device& device,
int platformIndex,
int deviceIndex,
bool verbose = false)
{
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);

if (!checkPlatformIndex(platforms, platformIndex)) {
return false;
}

const cl::Platform& platform = platforms[platformIndex];
printf("Running on platform: %s\n",
platform.getInfo<CL_PLATFORM_NAME>().c_str() );

std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);

if (!checkDeviceIndex(devices, deviceIndex)) {
return false;
}

device = devices[deviceIndex];
if (verbose) {
printf("Running on device: %s (%uCUs, %uMHz)\n",
device.getInfo<CL_DEVICE_NAME>().c_str(),
device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>(),
device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>() );
printf("Device version: %s\n",
device.getInfo<CL_DEVICE_VERSION>().c_str() );
printf("Driver version: %s\n",
device.getInfo<CL_DRIVER_VERSION>().c_str() );
} else {
printf("Running on device: %s\n",
device.getInfo<CL_DEVICE_NAME>().c_str() );
}

return true;
}
4 changes: 1 addition & 3 deletions samples/00_enumopencl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ static cl_int PrintDeviceInfoSummary(
return errorCode;
}

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
{
popl::OptionParser op("Supported Options");
Expand Down
4 changes: 1 addition & 3 deletions samples/00_enumopenclpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ static cl_int PrintDeviceInfoSummary(
return CL_SUCCESS;
}

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
{
popl::OptionParser op("Supported Options");
Expand Down
4 changes: 1 addition & 3 deletions samples/00_enumqueuefamilies/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ static void PrintDeviceInfoSummary(
}
}

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
{
popl::OptionParser op("Supported Options");
Expand Down
4 changes: 1 addition & 3 deletions samples/00_extendeddevicequeries/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ static void PrintDeviceInfoSummary(
}
}

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
{
popl::OptionParser op("Supported Options");
Expand Down
4 changes: 1 addition & 3 deletions samples/00_loaderinfo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ static void PrintLoaderInfo(const char* label, cl_icdl_info info)
printf("Query for for %s (size = %zu) returned: %s\n", label, sz, str.data());
}

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
{
popl::OptionParser op("Supported Options");
Expand Down
4 changes: 1 addition & 3 deletions samples/00_newqueries/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,7 @@ static void PrintDeviceInfoSummary(
}
}

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
{
popl::OptionParser op("Supported Options");
Expand Down
4 changes: 1 addition & 3 deletions samples/00_newqueriespp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,7 @@ static void PrintDeviceInfoSummary(
}
}

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
{
popl::OptionParser op("Supported Options");
Expand Down
4 changes: 1 addition & 3 deletions samples/00_spirvqueries/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

#include "util.hpp"

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
{
popl::OptionParser op("Supported Options");
Expand Down
27 changes: 8 additions & 19 deletions samples/01_copybuffer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

const size_t gwx = 1024*1024;

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
bool verbose = false;
int platformIndex = 0;
int deviceIndex = 0;

{
popl::OptionParser op("Supported Options");
op.add<popl::Switch, popl::Attribute::advanced>("v", "verbose", "Verbose Output", &verbose);
op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex);
op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex);

Expand All @@ -40,26 +40,15 @@ int main(
}
}

std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);

if (!checkPlatformIndex(platforms, platformIndex)) {
cl::Device device;
if (!setupDevice(device, platformIndex, deviceIndex, verbose)) {
return -1;
}

printf("Running on platform: %s\n",
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );

std::vector<cl::Device> devices;
platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices);

printf("Running on device: %s\n",
devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() );

cl::Context context{devices[deviceIndex]};
cl::CommandQueue commandQueue{context, devices[deviceIndex]};
cl::Context context{device};
cl::CommandQueue commandQueue{context, device};

cl::Buffer deviceMemSrc = cl::Buffer{
cl::Buffer deviceMemSrc = cl::Buffer{
context,
CL_MEM_ALLOC_HOST_PTR,
gwx * sizeof( cl_uint ) };
Expand Down
25 changes: 7 additions & 18 deletions samples/02_copybufferkernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ kernel void CopyBuffer( global uint* dst, global uint* src )
}
)CLC";

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
bool verbose = false;
int platformIndex = 0;
int deviceIndex = 0;

{
popl::OptionParser op("Supported Options");
op.add<popl::Switch, popl::Attribute::advanced>("v", "verbose", "Verbose Output", &verbose);
op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex);
op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex);

Expand All @@ -48,24 +48,13 @@ int main(
}
}

std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);

if (!checkPlatformIndex(platforms, platformIndex)) {
cl::Device device;
if (!setupDevice(device, platformIndex, deviceIndex, verbose)) {
return -1;
}

printf("Running on platform: %s\n",
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );

std::vector<cl::Device> devices;
platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices);

printf("Running on device: %s\n",
devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() );

cl::Context context{devices[deviceIndex]};
cl::CommandQueue commandQueue{context, devices[deviceIndex]};
cl::Context context{device};
cl::CommandQueue commandQueue{context, device};

cl::Program program{ context, kernelString };
program.build();
Expand Down
26 changes: 8 additions & 18 deletions samples/03_mandelbrot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ kernel void Mandelbrot(
}
)CLC";

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
bool verbose = false;
int platformIndex = 0;
int deviceIndex = 0;

{
popl::OptionParser op("Supported Options");
op.add<popl::Switch, popl::Attribute::advanced>("v", "verbose", "Verbose Output", &verbose);
op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex);
op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex);

bool printUsage = false;
try {
op.parse(argc, argv);
Expand All @@ -82,24 +83,13 @@ int main(
}
}

std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);

if (!checkPlatformIndex(platforms, platformIndex)) {
cl::Device device;
if (!setupDevice(device, platformIndex, deviceIndex, verbose)) {
return -1;
}

printf("Running on platform: %s\n",
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );

std::vector<cl::Device> devices;
platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices);

printf("Running on device: %s\n",
devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() );

cl::Context context{devices[deviceIndex]};
cl::CommandQueue commandQueue{context, devices[deviceIndex]};
cl::Context context{device};
cl::CommandQueue commandQueue{context, device};

cl::Program program{ context, kernelString };
program.build();
Expand Down
25 changes: 7 additions & 18 deletions samples/04_julia/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ kernel void Julia( global uchar4* dst, float cr, float ci )
}
)CLC";

int main(
int argc,
char** argv )
int main(int argc, char** argv)
{
bool verbose = false;
int platformIndex = 0;
int deviceIndex = 0;

Expand All @@ -79,6 +78,7 @@ int main(

{
popl::OptionParser op("Supported Options");
op.add<popl::Switch, popl::Attribute::advanced>("v", "verbose", "Verbose Output", &verbose);
op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex);
op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex);
op.add<popl::Value<size_t>>("i", "iterations", "Iterations", iterations, &iterations);
Expand All @@ -103,24 +103,13 @@ int main(
}
}

std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);

if (!checkPlatformIndex(platforms, platformIndex)) {
cl::Device device;
if (!setupDevice(device, platformIndex, deviceIndex, verbose)) {
return -1;
}

printf("Running on platform: %s\n",
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );

std::vector<cl::Device> devices;
platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices);

printf("Running on device: %s\n",
devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() );

cl::Context context{devices[deviceIndex]};
cl::CommandQueue commandQueue{context, devices[deviceIndex]};
cl::Context context{device};
cl::CommandQueue commandQueue{context, device};

cl::Program program{ context, kernelString };
program.build();
Expand Down
Loading