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

SCM Repository

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

View of /branches/pure-cfg/src/lib/cl-target/clinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1269 - (download) (as text) (annotate)
Sun Jun 5 11:46:52 2011 UTC (10 years, 6 months ago) by jhr
File size: 3352 byte(s)
  Working on OpenCL runtime support.  Also, added configure support for generating OpenCL
  runtime makefiles.
/*! \file clinfo.c
 *
 * \author John Reppy
 *
 * \brief This file contains functions that can be used to determine properties of the
 * host machine's OpenCL support.
 */

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

#include "clinfo.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_PLATFORMS	16

static bool GetDevices (PlatformInfo_t *plat);

STATIC_INLINE void *alloc (size_t szb)
{
    void *p = malloc(szb);
    if (p == 0) {
        fprintf (stderr, "fatal error: unable to allocate %d bytes of memory\n", (int)szb);
        exit (1);
    }
    return p;
}

#define NEW(ty)         (ty *)alloc(sizeof(ty))
#define NEWVEC(ty,n)    (ty *)alloc(sizeof(ty) * (n))
#define NEWSTR(s)       strcpy((char *)alloc(strlen(s)+1), s)

#define CHECK(call)     do {                                                    \
        cl_int __sts = call;                                                    \
        if (__sts != CL_SUCCESS) {                                              \
            fprintf(stderr, "error %d at %s:%d\n", __sts, __FILE__, __LINE__);  \
            exit (1);                                                           \
        }                                                                       \
    } while (0)


CLInfo_t *GetCLInfo ()
{
    cl_platform_id	platformIDs[MAX_PLATFORMS];
    char                buf[512];

  // get platform info
  // get number of OpenCL platforms
    cl_uint numPlatforms;
    CHECK(clGetPlatformIDs (MAX_PLATFORMS, platformIDs, &numPlatforms));

    PlatformInfo_t *plats = NEWVEC(PlatformInfo_t, numPlatforms);
    for (int i = 0;  i < numPlatforms;  i++) {
        clGetPlatformInfo (platformIDs[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0);
        plats[i].name = NEWSTR(buf);
        plats[i].id = platformIDs[i];
      // get OpenCL version info
        clGetPlatformInfo (platformIDs[i], CL_PLATFORM_VERSION, sizeof(buf), buf, 0);
// FIXME: parser version info
      // get extension info
// FIXME: TODO
      // get device info
        if (! GetDevices (&(plats[i]))) {
          // free storage
            for (int j = 0;  j <= i;  j++) {
                free (plats[i].name);
            }
            free (plats);
        }
    }

    CLInfo_t *info = NEW(CLInfo_t);
    info->numPlatforms = numPlatforms;
    info->platforms = plats;

    return info;

}

static bool GetDevices (PlatformInfo_t *plat)
{
    cl_uint             numDevs;
    char                buf[512];

  // get number of devices for the platform
    clGetDeviceIDs (plat->id, CL_DEVICE_TYPE_ALL, 0, 0, &numDevs);

    if (numDevs == 0) {
        plat->numDevices = 0;
        plat->devices = 0;
        return false;
    }

    plat->numDevices = numDevs;
    plat->devices = NEWVEC(DeviceInfo_t, numDevs);
    
    cl_device_id *devices = NEWVEC(cl_device_id, numDevs);
    CHECK(clGetDeviceIDs (plat->id, CL_DEVICE_TYPE_ALL, numDevs, devices, 0));

    for (int i = 0;  i < numDevs;  i++) {
        CHECK(clGetDeviceInfo (devices[i], CL_DEVICE_NAME, sizeof(buf), buf, 0));
        plat->devices[i].name = NEWSTR(buf);
        plat->devices[i].id = devices[i];
        CHECK(clGetDeviceInfo (
            devices[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &(plat->devices[i].ty), 0));
    }

    return true;
}

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