mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-06 14:16:39 +08:00
add function cl_get_device_id (#1948)
* add func cl_get_device_id cleanup * add fix from review old-commit-hash: d1588376171d8ba1d2ec1b670e085d6e15bd26c1
This commit is contained in:
@@ -1010,19 +1010,7 @@ cl_program build_pool_program(VisionState *s,
|
||||
|
||||
void cl_init(VisionState *s) {
|
||||
int err;
|
||||
cl_platform_id platform_id = NULL;
|
||||
cl_uint num_devices;
|
||||
cl_uint num_platforms;
|
||||
|
||||
err = clGetPlatformIDs(1, &platform_id, &num_platforms);
|
||||
assert(err == 0);
|
||||
err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1,
|
||||
&s->device_id, &num_devices);
|
||||
assert(err == 0);
|
||||
|
||||
cl_print_info(platform_id, s->device_id);
|
||||
printf("\n");
|
||||
|
||||
s->device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
|
||||
s->context = clCreateContext(NULL, 1, &s->device_id, NULL, NULL, &err);
|
||||
assert(err == 0);
|
||||
}
|
||||
|
||||
@@ -38,27 +38,8 @@ int main() {
|
||||
|
||||
|
||||
// init cl
|
||||
/* Get Platform and Device Info */
|
||||
cl_platform_id platform_id = NULL;
|
||||
cl_uint num_platforms_unused;
|
||||
int err = clGetPlatformIDs(1, &platform_id, &num_platforms_unused);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "cl error: %d\n", err);
|
||||
}
|
||||
assert(err == 0);
|
||||
|
||||
cl_device_id device_id = NULL;
|
||||
cl_uint num_devices_unused;
|
||||
err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id,
|
||||
&num_devices_unused);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "cl error: %d\n", err);
|
||||
}
|
||||
assert(err == 0);
|
||||
|
||||
cl_print_info(platform_id, device_id);
|
||||
printf("\n");
|
||||
|
||||
int err;
|
||||
cl_device_id device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
|
||||
cl_context context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err);
|
||||
assert(err == 0);
|
||||
|
||||
|
||||
@@ -42,14 +42,7 @@ static inline double millis_since_boot() {
|
||||
|
||||
void cl_init(cl_device_id &device_id, cl_context &context) {
|
||||
int err;
|
||||
cl_platform_id platform_id = NULL;
|
||||
cl_uint num_devices;
|
||||
cl_uint num_platforms;
|
||||
|
||||
err = clGetPlatformIDs(1, &platform_id, &num_platforms);
|
||||
err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1,
|
||||
&device_id, &num_devices);
|
||||
cl_print_info(platform_id, device_id);
|
||||
device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
|
||||
context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err);
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,44 @@ void clu_init(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
cl_device_id cl_get_device_id(cl_device_type device_type) {
|
||||
bool opencl_platform_found = false;
|
||||
cl_device_id device_id = NULL;
|
||||
|
||||
cl_uint num_platforms = 0;
|
||||
int err = clGetPlatformIDs(0, NULL, &num_platforms);
|
||||
assert(err == 0);
|
||||
cl_platform_id* platform_ids = malloc(sizeof(cl_platform_id) * num_platforms);
|
||||
err = clGetPlatformIDs(num_platforms, platform_ids, NULL);
|
||||
assert(err == 0);
|
||||
|
||||
char cBuffer[1024];
|
||||
for (size_t i = 0; i < num_platforms; i++) {
|
||||
err = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_NAME, sizeof(cBuffer), &cBuffer, NULL);
|
||||
assert(err == 0);
|
||||
printf("platform[%zu] CL_PLATFORM_NAME: %s\n", i, cBuffer);
|
||||
|
||||
cl_uint num_devices;
|
||||
err = clGetDeviceIDs(platform_ids[i], device_type, 0, NULL, &num_devices);
|
||||
if (err != 0 || !num_devices) {
|
||||
continue;
|
||||
}
|
||||
// Get first device
|
||||
err = clGetDeviceIDs(platform_ids[i], device_type, 1, &device_id, NULL);
|
||||
assert(err == 0);
|
||||
cl_print_info(platform_ids[i], device_id);
|
||||
opencl_platform_found = true;
|
||||
break;
|
||||
}
|
||||
free(platform_ids);
|
||||
|
||||
if (!opencl_platform_found) {
|
||||
printf("No valid openCL platform found\n");
|
||||
assert(opencl_platform_found);
|
||||
}
|
||||
return device_id;
|
||||
}
|
||||
|
||||
cl_program cl_create_program_from_file(cl_context ctx, const char* path) {
|
||||
char* src_buf = read_file(path, NULL);
|
||||
assert(src_buf);
|
||||
|
||||
@@ -17,6 +17,7 @@ extern "C" {
|
||||
|
||||
void clu_init(void);
|
||||
|
||||
cl_device_id cl_get_device_id(cl_device_type device_type);
|
||||
cl_program cl_create_program_from_file(cl_context ctx, const char* path);
|
||||
void cl_print_info(cl_platform_id platform, cl_device_id device);
|
||||
void cl_print_build_errors(cl_program program, cl_device_id device);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "common/visionbuf.h"
|
||||
#include "common/visionipc.h"
|
||||
#include "common/swaglog.h"
|
||||
#include "common/clutil.h"
|
||||
|
||||
#include "models/driving.h"
|
||||
#include "messaging.hpp"
|
||||
@@ -96,58 +97,12 @@ int main(int argc, char **argv) {
|
||||
#endif
|
||||
|
||||
// cl init
|
||||
cl_device_id device_id;
|
||||
cl_context context;
|
||||
cl_command_queue q;
|
||||
{
|
||||
cl_uint num_platforms;
|
||||
err = clGetPlatformIDs(0, NULL, &num_platforms);
|
||||
assert(err == 0);
|
||||
cl_device_id device_id = cl_get_device_id(device_type);
|
||||
cl_context context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err);
|
||||
assert(err == 0);
|
||||
|
||||
cl_platform_id * platform_ids = new cl_platform_id[num_platforms];
|
||||
err = clGetPlatformIDs(num_platforms, platform_ids, NULL);
|
||||
assert(err == 0);
|
||||
|
||||
LOGD("got %d opencl platform(s)", num_platforms);
|
||||
|
||||
char cBuffer[1024];
|
||||
bool opencl_platform_found = false;
|
||||
|
||||
for (size_t i = 0; i < num_platforms; i++){
|
||||
err = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_NAME, sizeof(cBuffer), &cBuffer, NULL);
|
||||
assert(err == 0);
|
||||
LOGD("platform[%zu] CL_PLATFORM_NAME: %s", i, cBuffer);
|
||||
|
||||
cl_uint num_devices;
|
||||
err = clGetDeviceIDs(platform_ids[i], device_type, 0, NULL, &num_devices);
|
||||
if (err != 0|| !num_devices){
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get first device
|
||||
err = clGetDeviceIDs(platform_ids[i], device_type, 1, &device_id, NULL);
|
||||
assert(err == 0);
|
||||
|
||||
context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err);
|
||||
assert(err == 0);
|
||||
|
||||
q = clCreateCommandQueue(context, device_id, 0, &err);
|
||||
assert(err == 0);
|
||||
|
||||
opencl_platform_found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
delete[] platform_ids;
|
||||
|
||||
if (!opencl_platform_found){
|
||||
LOGE("No valid openCL platform found");
|
||||
assert(opencl_platform_found);
|
||||
}
|
||||
|
||||
|
||||
LOGD("opencl init complete");
|
||||
}
|
||||
cl_command_queue q = clCreateCommandQueue(context, device_id, 0, &err);
|
||||
assert(err == 0);
|
||||
|
||||
// init the models
|
||||
ModelState model;
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:d967ffcacdd6c1262ca9769e464976cf772320be1bf1cae2b3c8cd8717cfb7a0
|
||||
size 24541
|
||||
oid sha256:b6fcf547efd5bd6d6a740d9b7415e7dacd6624910eb3447e1a2063b0bfb6fba8
|
||||
size 24327
|
||||
|
||||
@@ -33,28 +33,8 @@ typedef struct {
|
||||
|
||||
void initialize_opencl(VisionTest* visiontest) {
|
||||
// init cl
|
||||
/* Get Platform and Device Info */
|
||||
cl_platform_id platform_ids[16] = {0};
|
||||
cl_uint num_platforms;
|
||||
int err = clGetPlatformIDs(16, platform_ids, &num_platforms);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "cl error: %d\n", err);
|
||||
}
|
||||
assert(err == 0);
|
||||
|
||||
// try to find a CPU device
|
||||
cl_device_id device_id = NULL;
|
||||
for (int i=0; i<num_platforms; i++) {
|
||||
cl_uint num_devices_unused;
|
||||
err = clGetDeviceIDs(platform_ids[i], CL_DEVICE_TYPE_CPU, 1, &device_id,
|
||||
&num_devices_unused);
|
||||
if (err == 0) break;
|
||||
}
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "cl error: %d\n", err);
|
||||
}
|
||||
assert(err == 0);
|
||||
|
||||
int err;
|
||||
cl_device_id device_id = cl_get_device_id(CL_DEVICE_TYPE_CPU);
|
||||
visiontest->context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &err);
|
||||
assert(err == 0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user