Skip to content

Commit

Permalink
Fix c api allocator (Tencent#4360)
Browse files Browse the repository at this point in the history
* add some c_api interfaces related to allocator setup.

* fix errors in allocator parameters in c_api.

* test c api allocator

Co-authored-by: zhangtongshe <yuyuyezi@vip.qq.com>
  • Loading branch information
2 people authored and joeyballentine committed Jun 18, 2023
1 parent 6154630 commit 128a8a6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 deletions.
82 changes: 55 additions & 27 deletions src/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ ncnn_allocator_t ncnn_allocator_create_unlocked_pool_allocator()

void ncnn_allocator_destroy(ncnn_allocator_t allocator)
{
delete (Allocator*)allocator->pthis;
free(allocator);
if (allocator)
{
delete (Allocator*)allocator->pthis;
free(allocator);
}
}

/* option api */
Expand All @@ -163,6 +166,26 @@ void ncnn_option_set_num_threads(ncnn_option_t opt, int num_threads)
((Option*)opt)->num_threads = num_threads;
}

int ncnn_option_get_use_local_pool_allocator(const ncnn_option_t opt)
{
return ((Option*)opt)->use_local_pool_allocator;
}

void ncnn_option_set_use_local_pool_allocator(ncnn_option_t opt, int use_local_pool_allocator)
{
((Option*)opt)->use_local_pool_allocator = use_local_pool_allocator;
}

void ncnn_option_set_blob_allocator(ncnn_option_t opt, ncnn_allocator_t allocator)
{
((Option*)opt)->blob_allocator = allocator ? (Allocator*)allocator->pthis : NULL;
}

void ncnn_option_set_workspace_allocator(ncnn_option_t opt, ncnn_allocator_t allocator)
{
((Option*)opt)->workspace_allocator = allocator ? (Allocator*)allocator->pthis : NULL;
}

int ncnn_option_get_use_vulkan_compute(const ncnn_option_t opt)
{
return ((const Option*)opt)->use_vulkan_compute;
Expand All @@ -181,82 +204,82 @@ ncnn_mat_t ncnn_mat_create()

ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, (size_t)4u, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, (size_t)4u, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, c, (size_t)4u, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, c, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_4d(int w, int h, int d, int c, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, d, c, (size_t)4u, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, d, c, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, data, (size_t)4u, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, data, (size_t)4u, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, c, data, (size_t)4u, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, c, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_external_4d(int w, int h, int d, int c, void* data, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, d, c, data, (size_t)4u, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, d, c, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, elemsize, elempack, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, elemsize, elempack, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, c, elemsize, elempack, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, c, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_4d_elem(int w, int h, int d, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, d, c, elemsize, elempack, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, d, c, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, data, elemsize, elempack, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, data, elemsize, elempack, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_external_3d_elem(int w, int h, int c, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, c, data, elemsize, elempack, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, c, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
}

ncnn_mat_t ncnn_mat_create_external_4d_elem(int w, int h, int d, int c, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, d, c, data, elemsize, elempack, (Allocator*)allocator));
return (ncnn_mat_t)(new Mat(w, h, d, c, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
}

void ncnn_mat_destroy(ncnn_mat_t mat)
Expand All @@ -271,27 +294,27 @@ void ncnn_mat_fill_float(ncnn_mat_t mat, float v)

ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->clone((Allocator*)allocator)));
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->clone(allocator ? (Allocator*)allocator->pthis : NULL)));
}

ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, (Allocator*)allocator)));
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, allocator ? (Allocator*)allocator->pthis : NULL)));
}

ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, (Allocator*)allocator)));
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, allocator ? (Allocator*)allocator->pthis : NULL)));
}

ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, c, (Allocator*)allocator)));
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, c, allocator ? (Allocator*)allocator->pthis : NULL)));
}

ncnn_mat_t ncnn_mat_reshape_4d(const ncnn_mat_t mat, int w, int h, int d, int c, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, d, c, (Allocator*)allocator)));
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, d, c, allocator ? (Allocator*)allocator->pthis : NULL)));
}

int ncnn_mat_get_dims(const ncnn_mat_t mat)
Expand Down Expand Up @@ -349,22 +372,22 @@ void* ncnn_mat_get_channel_data(const ncnn_mat_t mat, int c)
/* mat pixel api */
ncnn_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(Mat::from_pixels(pixels, type, w, h, stride, (Allocator*)allocator)));
return (ncnn_mat_t)(new Mat(Mat::from_pixels(pixels, type, w, h, stride, allocator ? (Allocator*)allocator->pthis : NULL)));
}

ncnn_mat_t ncnn_mat_from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int stride, int target_width, int target_height, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(Mat::from_pixels_resize(pixels, type, w, h, stride, target_width, target_height, (Allocator*)allocator)));
return (ncnn_mat_t)(new Mat(Mat::from_pixels_resize(pixels, type, w, h, stride, target_width, target_height, allocator ? (Allocator*)allocator->pthis : NULL)));
}

ncnn_mat_t ncnn_mat_from_pixels_roi(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(Mat::from_pixels_roi(pixels, type, w, h, stride, roix, roiy, roiw, roih, (Allocator*)allocator)));
return (ncnn_mat_t)(new Mat(Mat::from_pixels_roi(pixels, type, w, h, stride, roix, roiy, roiw, roih, allocator ? (Allocator*)allocator->pthis : NULL)));
}

ncnn_mat_t ncnn_mat_from_pixels_roi_resize(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, int target_width, int target_height, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(Mat::from_pixels_roi_resize(pixels, type, w, h, stride, roix, roiy, roiw, roih, target_width, target_height, (Allocator*)allocator)));
return (ncnn_mat_t)(new Mat(Mat::from_pixels_roi_resize(pixels, type, w, h, stride, roix, roiy, roiw, roih, target_width, target_height, allocator ? (Allocator*)allocator->pthis : NULL)));
}

void ncnn_mat_to_pixels(const ncnn_mat_t mat, unsigned char* pixels, int type, int stride)
Expand Down Expand Up @@ -1180,6 +1203,11 @@ void ncnn_net_destroy(ncnn_net_t net)
free(net);
}

ncnn_option_t ncnn_net_get_option(ncnn_net_t net)
{
return (ncnn_option_t)(&((Net*)(net->pthis))->opt);
}

void ncnn_net_set_option(ncnn_net_t net, ncnn_option_t opt)
{
((Net*)net->pthis)->opt = *((Option*)opt);
Expand Down
7 changes: 7 additions & 0 deletions src/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ NCNN_EXPORT void ncnn_option_destroy(ncnn_option_t opt);
NCNN_EXPORT int ncnn_option_get_num_threads(const ncnn_option_t opt);
NCNN_EXPORT void ncnn_option_set_num_threads(ncnn_option_t opt, int num_threads);

NCNN_EXPORT int ncnn_option_get_use_local_pool_allocator(const ncnn_option_t opt);
NCNN_EXPORT void ncnn_option_set_use_local_pool_allocator(ncnn_option_t opt, int use_local_pool_allocator);

NCNN_EXPORT void ncnn_option_set_blob_allocator(ncnn_option_t opt, ncnn_allocator_t allocator);
NCNN_EXPORT void ncnn_option_set_workspace_allocator(ncnn_option_t opt, ncnn_allocator_t allocator);

NCNN_EXPORT int ncnn_option_get_use_vulkan_compute(const ncnn_option_t opt);
NCNN_EXPORT void ncnn_option_set_use_vulkan_compute(ncnn_option_t opt, int use_vulkan_compute);

Expand Down Expand Up @@ -265,6 +271,7 @@ struct __ncnn_net_t
NCNN_EXPORT ncnn_net_t ncnn_net_create();
NCNN_EXPORT void ncnn_net_destroy(ncnn_net_t net);

NCNN_EXPORT ncnn_option_t ncnn_net_get_option(ncnn_net_t net);
NCNN_EXPORT void ncnn_net_set_option(ncnn_net_t net, ncnn_option_t opt);

#if NCNN_STRING
Expand Down
13 changes: 11 additions & 2 deletions tests/test_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,15 @@ static int test_c_api_2()
emptydr->read = emptydr_read;
}

ncnn_allocator_t blob_allocator = ncnn_allocator_create_pool_allocator();
ncnn_allocator_t workspace_allocator = ncnn_allocator_create_unlocked_pool_allocator();

ncnn_option_t opt = ncnn_option_create();
{
ncnn_option_set_num_threads(opt, 1);

ncnn_option_set_blob_allocator(opt, blob_allocator);
ncnn_option_set_workspace_allocator(opt, workspace_allocator);
}

ncnn_net_t net = ncnn_net_create();
Expand All @@ -260,7 +266,7 @@ static int test_c_api_2()
ncnn_net_load_model_datareader(net, emptydr);
}

ncnn_mat_t a = ncnn_mat_create_1d(24, NULL);
ncnn_mat_t a = ncnn_mat_create_1d(24, blob_allocator);

// set a
{
Expand All @@ -274,7 +280,7 @@ static int test_c_api_2()
memcpy(a_data, data, 24 * sizeof(float));
}

ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, NULL);
ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, blob_allocator);
ncnn_mat_t c = 0;

{
Expand Down Expand Up @@ -321,6 +327,9 @@ static int test_c_api_2()

ncnn_option_destroy(opt);

ncnn_allocator_destroy(blob_allocator);
ncnn_allocator_destroy(workspace_allocator);

ncnn_datareader_destroy(emptydr);

if (!success)
Expand Down

0 comments on commit 128a8a6

Please sign in to comment.