thneed/serialize: use funtions from clutil(#22760)

pull/22795/head
Dean Lee 2021-11-04 23:19:25 +08:00 committed by GitHub
parent ff33ca3413
commit 177c3b89b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 41 deletions

View File

@ -1,12 +1,8 @@
#include "selfdrive/common/clutil.h"
#include <sys/stat.h>
#include <cassert>
#include <cstring>
#include <iostream>
#include <memory>
#include <vector>
#include "selfdrive/common/util.h"
@ -78,8 +74,10 @@ cl_device_id cl_get_device_id(cl_device_type device_type) {
}
cl_program cl_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args) {
std::string src = util::read_file(path);
assert(src.length() > 0);
return cl_program_from_source(ctx, device_id, util::read_file(path), args);
}
cl_program cl_program_from_source(cl_context ctx, cl_device_id device_id, const std::string& src, const char* args) {
cl_program prg = CL_CHECK_ERR(clCreateProgramWithSource(ctx, 1, (const char*[]){src.c_str()}, NULL, &err));
if (int err = clBuildProgram(prg, 1, &device_id, args, NULL, NULL); err != 0) {
cl_print_build_errors(prg, device_id);
@ -88,6 +86,15 @@ cl_program cl_program_from_file(cl_context ctx, cl_device_id device_id, const ch
return prg;
}
cl_program cl_program_from_binary(cl_context ctx, cl_device_id device_id, const uint8_t* binary, size_t length, const char* args) {
cl_program prg = CL_CHECK_ERR(clCreateProgramWithBinary(ctx, 1, &device_id, &length, (const uint8_t*[]){binary}, NULL, &err));
if (int err = clBuildProgram(prg, 1, &device_id, args, NULL, NULL); err != 0) {
cl_print_build_errors(prg, device_id);
assert(0);
}
return prg;
}
// Given a cl code and return a string representation
#define CL_ERR_TO_STR(err) case err: return #err
const char* cl_get_error_string(int err) {

View File

@ -1,14 +1,13 @@
#pragma once
#include <cstdint>
#include <cstdlib>
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include <string>
#define CL_CHECK(_expr) \
do { \
assert(CL_SUCCESS == (_expr)); \
@ -23,5 +22,7 @@
})
cl_device_id cl_get_device_id(cl_device_type device_type);
cl_program cl_program_from_source(cl_context ctx, cl_device_id device_id, const std::string& src, const char* args = nullptr);
cl_program cl_program_from_binary(cl_context ctx, cl_device_id device_id, const uint8_t* binary, size_t length, const char* args = nullptr);
cl_program cl_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args);
const char* cl_get_error_string(int err);

View File

@ -3,6 +3,7 @@
#include "json11.hpp"
#include "selfdrive/common/util.h"
#include "selfdrive/common/clutil.h"
#include "selfdrive/modeld/thneed/thneed.h"
using namespace json11;
@ -61,44 +62,17 @@ void Thneed::load(const char *filename) {
}
map<string, cl_program> g_programs;
for (auto &obj : jdat["programs"].object_items()) {
const char *srcs[1];
srcs[0] = (const char *)obj.second.string_value().c_str();
size_t length = obj.second.string_value().size();
if (record & THNEED_DEBUG) printf("building %s with size %zu\n", obj.first.c_str(), length);
cl_program program = clCreateProgramWithSource(context, 1, srcs, &length, NULL);
int err_ = clBuildProgram(program, 1, &device_id, "", NULL, NULL);
if (err_ != 0) {
printf("got err %d\n", err_);
size_t length_;
char buffer[2048];
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &length_);
buffer[length_] = '\0';
printf("%s\n", buffer);
}
assert(err_ == 0);
g_programs[obj.first] = program;
for (const auto &[name, source] : jdat["programs"].object_items()) {
if (record & THNEED_DEBUG) printf("building %s with size %zu\n", name.c_str(), source.string_value().size());
g_programs[name] = cl_program_from_source(context, device_id, source.string_value());
}
for (auto &obj : jdat["binaries"].array_items()) {
string name = obj["name"].string_value();
size_t length = obj["length"].int_value();
const unsigned char *srcs[1];
srcs[0] = (const unsigned char *)&buf[ptr];
ptr += length;
if (record & THNEED_DEBUG) printf("binary %s with size %zu\n", name.c_str(), length);
cl_int err_;
cl_program program = clCreateProgramWithBinary(context, 1, &device_id, &length, srcs, NULL, &err_);
assert(program != NULL && err_ == CL_SUCCESS);
err_ = clBuildProgram(program, 1, &device_id, "", NULL, NULL);
assert(err_ == CL_SUCCESS);
g_programs[name] = program;
g_programs[name] = cl_program_from_binary(context, device_id, (const uint8_t*)&buf[ptr], length);
ptr += length;
}
for (auto &obj : jdat["kernels"].array_items()) {