Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/pure-cfg/src/clinfo/clinfo.c
ViewVC logotype

View of /branches/pure-cfg/src/clinfo/clinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1251 - (download) (as text) (annotate)
Thu May 19 13:26:43 2011 UTC (8 years, 4 months ago) by jhr
File size: 3796 byte(s)
  Working on clinfo
/*! \file clinfo.c
 *
 * \author John Reppy
 *
 * \brief This program is used to determine properties of the host machine's
 * OpenCL support.
 *
 * Usage:
 *	clinfo cmd [args]
 *
 *	where cmd is one of
 *		platforms		-- prints a list of OpenCL platforms supported by the
 *					   host.
 *		version	platform	-- prints version of OpenCL supported by the platform.  The
 *					   format is "OpenCL major.minor ..."
 *		devices	platform	-- prints a list of devices
 *		extensions device	-- prints a list of extensions that are
 *					   supported by the device
 */

/*
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */

#include "Diderot/config.h"
# ifdef HAVE_CL_CL_H
#   include <CL/cl.h>
# elif defined(HAVE_OPENCL_CL_H)
#   include <OpenCL/cl.h>
# else
#   error no cl.h
# endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_PLATFORMS	16

static void usage ();
static int GetPlatforms (int max, cl_platform_id *platforms);
static cl_platform_id GetPlatformByName (const char *name);
static int GetDevices (cl_platform_id plat, cl_device_type ty, cl_device_id **devs);

int main (int argc, const char **argv)
{
    cl_platform_id	platforms[MAX_PLATFORMS];

  // process command-line options
    if ((strcmp(argv[1], "platforms") == 0) && (argc == 2)) {
	char buf[256];
	int numPlatforms = GetPlatforms (MAX_PLATFORMS, platforms);
	for (int i = 0;  i < numPlatforms;  i++) {
	    clGetPlatformInfo (platforms[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0);
	    printf ("%s\n", buf);
	}
    }
    else if ((strcmp(argv[1], "version") == 0) && (argc == 3)) {
	char buf[256];
	cl_platform_id id = GetPlatformByName(argv[2]);
	clGetPlatformInfo (id, CL_PLATFORM_VERSION, sizeof(buf), buf, 0);
	printf ("%s\n", buf);
    }
    else if ((strcmp(argv[1], "devices") == 0) && (argc == 3)) {
	cl_platform_id id = GetPlatformByName(argv[2]);
	cl_device_id *devs;
	char buf[256];
	int numDevs = GetDevices (id, CL_DEVICE_TYPE_ALL, &devs);
	for (int i = 0;  i < numDevs;  i++) {
	    clGetDeviceInfo (devs[i], CL_DEVICE_NAME, sizeof(buf), buf, 0);
	    printf ("%s\n", buf);
	}
    }
    else if ((strcmp(argv[1], "extensions") == 0) && (argc == 3)) {
	cl_platform_id id = GetPlatformByName(argv[2]);
	size_t bufSz;
      // get the size of the extensions buffer
	clGetPlatformInfo (id, CL_PLATFORM_EXTENSIONS, 0, 0, &bufSz);
	char *buf = (char *)malloc(bufSz);
	clGetPlatformInfo (id, CL_PLATFORM_EXTENSIONS, bufSz, buf, 0);
	char *p = buf;
	while (p != 0) {
	    p = strchr(p, ' ');
	    if (p != 0) *p = '\n';
	}
	printf ("%s", buf);
	free (buf);
    }
    else
	usage();

}

static void usage ()
{
    exit (1);
}

static int GetPlatforms (int max, cl_platform_id *platforms)
{
    cl_uint n;
  // get number of OpenCL platforms
    if (clGetPlatformIDs (max, platforms, &n) != CL_SUCCESS) {
	fprintf(stderr, "unable to get platform IDs\n");
	exit (1);
    }
    return n;
}

static cl_platform_id GetPlatformByName (const char *name)
{
    cl_platform_id platforms[MAX_PLATFORMS];
    char buf[256];

  // get number of OpenCL platforms
    cl_uint n = GetPlatforms (MAX_PLATFORMS, platforms);
    for (cl_uint i = 0;  i < n;  i++) {
	clGetPlatformInfo (platforms[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0);
	if (strcmp(buf, name) == 0) return platforms[i];
    }

    fprintf (stderr, "platform %s not found\n", name);
    exit (1);

}

static int GetDevices (cl_platform_id plat, cl_device_type ty, cl_device_id **devs)
{
    cl_uint numDevs;

  // get number of devices for the platform
    clGetDeviceIDs (plat, ty, 0, 0, &numDevs);

    cl_device_id *devices = (cl_device_id *)malloc(numDevs * sizeof(cl_device_id));
    clGetDeviceIDs (plat, ty, numDevs, devices, 0);

    *devs = devices;
    return numDevs;
}

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0