Example #1
0
/**
 * Device info main program function.
 *
 * @param[in] argc Number of command line arguments.
 * @param[in] argv Vector of command line arguments.
 * @return ::CCL_SUCCESS if program returns with no error, or another
 * ::CCLErrorCode value otherwise.
 */
int main(int argc, char* argv[]) {

	/* Error object. */
	CCLErr* err = NULL;

	/* List of platform wrapper objects. */
	CCLPlatforms* platforms = NULL;

	/* List of device wrapper objects. */
	CCLDevSelDevices devices = NULL;

	/* Current platform and device. */
	CCLPlatform* p;
	CCLDevice* d;

	/* Number of devices in platform. */
	guint num_devs;

	/* Device information value object. */
	CCLWrapperInfo* info_value = NULL;

	/* Device name. */
	gchar* dev_name;

	/* Program return status. */
	gint status;

	/* Parse command line options. */
	ccl_devinfo_args_parse(argc, argv, &err);
	ccl_if_err_goto(err, error_handler);

	/* If version was requested, output version and exit. */
	if (version) {
		ccl_common_version_print("ccl_devinfo");
		exit(0);
	}

	/* Check if user requested a list of known information parameters. */
	if (opt_list) {

		/*Yes, user requested list, present it. */

		g_fprintf(CCL_DEVINFO_OUT, "\nKnown information parameters:\n\n");
		for (gint i = 0; i < ccl_devquery_info_map_size; i++) {
			if (opt_verb) {
				g_fprintf(CCL_DEVINFO_OUT,
					"\t%s\n\t\t%s.\n\n",
					ccl_devquery_info_map[i].param_name,
					ccl_devquery_info_map[i].description);
			} else {
				g_fprintf(CCL_DEVINFO_OUT,
					"\t%s\n",
					ccl_devquery_info_map[i].param_name);
			}
		}
		g_fprintf(CCL_DEVINFO_OUT, "\n");

	} else {

		/* User didn't request list, proceed as normal query. */

		/* Ignore platforms and focus only on number of devices in system? */
		if (no_platf) {

			/* Ignore platform, continue device-wise. */

			/* Get all devices in the system. */
			devices = ccl_devsel_devices_new(&err);
			ccl_if_err_goto(err, error_handler);

			/* Cycle through devices. */
			for (guint j = 0; j < devices->len; j++) {

				/* Get out if this device is not to be queried. */
				if ((opt_dev != G_MAXUINT) && (j != opt_dev))
					continue;

				/* Get current device. */
				d = (CCLDevice*) devices->pdata[j];

				/* Get device name. */
				info_value = ccl_device_get_info(d, CL_DEVICE_NAME, &err);
				ccl_if_err_goto(err, error_handler);

				dev_name = (gchar*) info_value->value;

				/* Show device information. */
				g_fprintf(CCL_DEVINFO_OUT,
					"\n    [ Device #%d: %s ]\n\n",
					j, dev_name);
				ccl_devinfo_show_device_info(d);

			}
			g_fprintf(CCL_DEVINFO_OUT, "\n");

		} else {

			/* Do not ignore platforms, continue platform-wise. */

			/* Get list of platform wrapper objects. */
			platforms = ccl_platforms_new(&err);
			ccl_if_err_goto(err, error_handler);

			/* Cycle through platforms. */
			for (guint i = 0; i < ccl_platforms_count(platforms); i++) {

				/* Get out if this platform is not to be queried. */
				if ((opt_platf != G_MAXUINT) && (i != opt_platf))
					continue;

				/* Get current platform. */
				p = ccl_platforms_get(platforms, i);

				/* Show platform information. */
				ccl_devinfo_show_platform_info(p, i);

				/* Get number of devices. */
				num_devs = ccl_platform_get_num_devices(p, &err);

				/* Is this a platform without devices? */
				if ((err) && (err->domain == CCL_OCL_ERROR) &&
						(err->code == CL_DEVICE_NOT_FOUND)) {

					/* Clear "device not found" error. */
					g_clear_error(&err);

					/* Inform about non-existing devices. */
					g_fprintf(CCL_DEVINFO_OUT,
						"\n    [ No devices found ]\n\n");

					/* Skip this platform. */
					continue;
				}
				ccl_if_err_goto(err, error_handler);

				/* Cycle through devices. */
				for (guint j = 0; j < num_devs; j++) {

					/* Get out if this device is not to be queried. */
					if ((opt_dev != G_MAXUINT) && (j != opt_dev))
						continue;

					/* Get current device. */
					d = ccl_platform_get_device(p, j, &err);
					ccl_if_err_goto(err, error_handler);

					/* Get device name. */
					info_value = ccl_device_get_info(d, CL_DEVICE_NAME, &err);
					ccl_if_err_goto(err, error_handler);

					dev_name = (gchar*) info_value->value;

					/* Show device information. */
					g_fprintf(CCL_DEVINFO_OUT,
						"\n    [ Device #%d: %s ]\n\n",
						j, dev_name);
					ccl_devinfo_show_device_info(d);

				}
				g_fprintf(CCL_DEVINFO_OUT, "\n");
			}
		}
	}

	/* If we got here, everything is OK. */
	g_assert(err == NULL);
	status = CCL_SUCCESS;
	goto cleanup;

error_handler:

	/* If we got here there was an error, verify that it is so. */
	g_assert(err != NULL);
	g_fprintf(stderr, "%s\n", err->message);
	status = (err->domain == CCL_ERROR) ? err->code : CCL_ERROR_OTHER;
	g_error_free(err);

cleanup:

	/* Free stuff! */
	if (platforms) ccl_platforms_destroy(platforms);
	if (devices) ccl_devsel_devices_destroy(devices);
	g_strfreev(opt_custom);

	/* Confirm that memory allocated by wrappers has been properly
	 * freed. */
	g_return_val_if_fail(ccl_wrapper_memcheck(), CCL_ERROR_OTHER);

	/* Return status. */
	return status;

}
Example #2
0
/**
 * @internal
 *
 * @brief Tests buffer fill.
 * */
static void fill_test() {

    /* Test variables. */
    CCLPlatforms * ps;
    CCLPlatform * p;
    CCLContext * ctx = NULL;
    CCLDevice * d = NULL;
    CCLBuffer * b = NULL;
    CCLQueue * q;
    cl_char8 h[CCL_TEST_BUFFER_SIZE];
    cl_char8 pattern = {{ 1, -1, 5, 4, -12, 3, 7, -20 }};
    size_t buf_size = sizeof(cl_char8) * CCL_TEST_BUFFER_SIZE;
    CCLErr * err = NULL;

    /* Get a context which supports OpenCL 1.2, if possible. */
    ps = ccl_platforms_new(&err);
    g_assert_no_error(err);
    for (guint i = 0; i < ccl_platforms_count(ps); ++i) {
        p = ccl_platforms_get(ps, i);
        cl_uint ocl_ver = ccl_platform_get_opencl_version(p, &err);
        if (ocl_ver >= 120) {
            ctx = ccl_context_new_from_devices(
                ccl_platform_get_num_devices(p, NULL),
                ccl_platform_get_all_devices(p, NULL),
                &err);
            g_assert_no_error(err);
            break;
        }
    }

    /* If not possible to find a 1.2 or better context, finish this
     * test. */
    if (ctx == NULL) {
        g_test_message("'%s' test not performed because no platform " \
            "with OpenCL 1.2 support was found", CCL_STRD);
        ccl_platforms_destroy(ps);
        return;
    }

    /* Get first device in context. */
    d = ccl_context_get_device(ctx, 0, &err);
    g_assert_no_error(err);

    /* Create a command queue. */
    q = ccl_queue_new(ctx, d, 0, &err);
    g_assert_no_error(err);

    /* Create regular buffer. */
    b = ccl_buffer_new(ctx, CL_MEM_READ_WRITE, buf_size, NULL, &err);
    g_assert_no_error(err);

    /* Fill buffer with pattern. */
    ccl_buffer_enqueue_fill(
        b, q, &pattern, sizeof(cl_char8), 0, buf_size, NULL, &err);
    g_assert_no_error(err);

    /* Read data back to host. */
    ccl_buffer_enqueue_read(b, q, CL_TRUE, 0, buf_size, h, NULL, &err);
    g_assert_no_error(err);

    /* Check data is OK. */
    for (guint i = 0; i < CCL_TEST_BUFFER_SIZE; ++i)
        for (guint j = 0; j < 8; ++j)
            g_assert_cmpuint(h[i].s[j], ==, pattern.s[j]);

    /* Free stuff. */
    ccl_buffer_destroy(b);
    ccl_queue_destroy(q);
    ccl_context_destroy(ctx);
    ccl_platforms_destroy(ps);

    /* Confirm that memory allocated by wrappers has been properly
     * freed. */
    g_assert(ccl_wrapper_memcheck());
}
Example #3
0
/**
 * @internal
 *
 * @brief Tests memory object migration.
 * */
static void migrate_test() {

    /* Test variables. */
    CCLPlatforms * ps;
    CCLPlatform * p;
    CCLContext * ctx = NULL;
    CCLDevice * d = NULL;
    CCLBuffer * b = NULL;
    CCLQueue * q;
    size_t buf_size = sizeof(cl_char8) * CCL_TEST_BUFFER_SIZE;
    CCLErr * err = NULL;

    /* Get a context which supports OpenCL 1.2 if possible. */
    ps = ccl_platforms_new(&err);
    g_assert_no_error(err);
    for (guint i = 0; i < ccl_platforms_count(ps); ++i) {
        p = ccl_platforms_get(ps, i);
        cl_uint ocl_ver = ccl_platform_get_opencl_version(p, &err);
        if (ocl_ver >= 120) {
            ctx = ccl_context_new_from_devices(
                ccl_platform_get_num_devices(p, NULL),
                ccl_platform_get_all_devices(p, NULL),
                &err);
            g_assert_no_error(err);
            break;
        }
    }

    /* If not possible to find a 1.2 or better context, finish this
     * test. */
    if (ctx == NULL) {
        g_test_message("'%s' test not performed because no platform " \
            "with OpenCL 1.2 support was found", CCL_STRD);
        ccl_platforms_destroy(ps);
        return;
    }

    /* Get first device in context. */
    d = ccl_context_get_device(ctx, 0, &err);
    g_assert_no_error(err);

    /* Create a command queue associated with first device in
     * context. */
    q = ccl_queue_new(ctx, d, 0, &err);
    g_assert_no_error(err);

    /* Create regular buffer. */
    b = ccl_buffer_new(ctx, CL_MEM_READ_WRITE, buf_size, NULL, &err);
    g_assert_no_error(err);

    /* Assign buffer to first device in context (via the command
     * queue). */
    ccl_memobj_enqueue_migrate((CCLMemObj **) &b, 1, q, 0, NULL, &err);
    g_assert_no_error(err);

    /* Migrate buffer to host. */
    ccl_memobj_enqueue_migrate(
        (CCLMemObj **) &b, 1, q, CL_MIGRATE_MEM_OBJECT_HOST, NULL, &err);
    g_assert_no_error(err);

    /* Wait for queue to finish... */
    ccl_queue_finish(q, &err);
    g_assert_no_error(err);

    /* Free stuff. */
    ccl_buffer_destroy(b);
    ccl_queue_destroy(q);
    ccl_context_destroy(ctx);
    ccl_platforms_destroy(ps);

    /* Confirm that memory allocated by wrappers has been properly
     * freed. */
    g_assert(ccl_wrapper_memcheck());
}
Example #4
0
/**
 * Tests devquery module helper functions.
 * */
static void helpers_test() {

	CCLPlatforms* platfs = NULL;
	CCLPlatform* p = NULL;
	CCLDevice* d = NULL;
	GError* err = NULL;
	guint num_devs;
	guint num_platfs;
	CCLWrapperInfo* info;
	gchar param_value_str[CCL_TEST_DEVQUERY_MAXINFOLEN];

	/* Get platforms. */
	platfs = ccl_platforms_new(&err);
	if (err == NULL) {

		/* Number of platforms. */
		num_platfs = ccl_platforms_count(platfs);
		g_debug("* Found %d OpenCL platforms", num_platfs);

		/* Cycle through platforms. */
		for (guint i = 0; i < num_platfs; i++) {

			/* Get current platform. */
			p = ccl_platforms_get(platfs, i);
			g_debug(">> Platform %d:", i);

			/* Get number of devices. */
			num_devs = ccl_platform_get_num_devices(p, &err);

			/* Only test for device information if device count was
			 * successfully obtained. */
			if (err != NULL) {
				g_test_message("Error obtaining number of devices for platform %d (%s).",
					i, err->message);
				g_clear_error(&err);
			} else {

				g_debug("==== # Devs  : %d", num_devs);

				/* Cycle through devices in platform. */
				for (guint j = 0; j < num_devs; j++) {

					/* Get current device. */
					d = ccl_platform_get_device(p, j, &err);
					g_assert_no_error(err);
					g_debug("====== Device #%d", j);

					for (gint k = 0; k < ccl_devquery_info_map_size; k++) {
						info = ccl_device_get_info(d, ccl_devquery_info_map[k].device_info, &err);
						if (err == NULL) {
							g_debug("\t%s : %s",
								ccl_devquery_info_map[k].param_name,
								ccl_devquery_info_map[k].format(
									info, param_value_str,
									CCL_TEST_DEVQUERY_MAXINFOLEN,
									ccl_devquery_info_map[k].units));
						} else {
							g_clear_error(&err);
							g_debug("\t%s : %s",
								ccl_devquery_info_map[k].param_name, "N/A");

						}
					}

				}
			}
		}

		/* Destroy list of platforms. */
		ccl_platforms_destroy(platfs);

	} else {

		/* Unable to get any OpenCL platforms, test can't pass. */
		g_test_message("Test failed due to following error: %s",
			err->message);
		g_test_fail();
	}

	/* Confirm that memory allocated by wrappers has been properly
	 * freed. */
	g_assert(ccl_wrapper_memcheck());

}