Skip to content

Commit

Permalink
update c api allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui committed Dec 25, 2020
1 parent 017440c commit 2b7b92b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 86 deletions.
2 changes: 1 addition & 1 deletion examples/squeezenet_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
ncnn_net_load_param(squeezenet, "squeezenet_v1.1.param");
ncnn_net_load_model(squeezenet, "squeezenet_v1.1.bin");

ncnn_mat_t in = ncnn_mat_from_pixels_resize(bgr.data, NCNN_MAT_PIXEL_BGR, bgr.cols, bgr.rows, bgr.cols * 3, 227, 227);
ncnn_mat_t in = ncnn_mat_from_pixels_resize(bgr.data, NCNN_MAT_PIXEL_BGR, bgr.cols, bgr.rows, bgr.cols * 3, 227, 227, NULL);

const float mean_vals[3] = {104.f, 117.f, 123.f};
ncnn_mat_substract_mean_normalize(in, mean_vals, 0);
Expand Down
97 changes: 40 additions & 57 deletions src/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,6 @@ void ncnn_allocator_fast_free(ncnn_allocator_t allocator, void* ptr)
((Allocator*)allocator)->fastFree(ptr);
}

static ncnn::ThreadLocalStorage tls_allocator;

ncnn_allocator_t ncnn_allocator_get_tls_allocator()
{
return (ncnn_allocator_t)tls_allocator.get();
}

void ncnn_allocator_set_tls_allocator(ncnn_allocator_t allocator)
{
tls_allocator.set((void*)allocator);
}

/* option api */
ncnn_option_t ncnn_option_create()
{
Expand Down Expand Up @@ -125,69 +113,64 @@ void ncnn_option_set_use_vulkan_compute(ncnn_option_t opt, int use_vulkan_comput
}

/* mat api */
ncnn_mat_t ncnn_mat_create()
{
return (ncnn_mat_t)(new Mat());
}

ncnn_mat_t ncnn_mat_create_1d(int w)
ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, 4u, (Allocator*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, 4u, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_2d(int w, int h)
ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, h, 4u, (Allocator*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, h, 4u, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c)
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, 4u, (Allocator*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, h, c, 4u, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data)
ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data, ncnn_allocator_t allocator)
{
return (ncnn_mat_t)(new Mat(w, data, 4u, (Allocator*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, data, 4u, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data)
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, 4u, (Allocator*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, h, data, 4u, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data)
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, 4u, (Allocator*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, h, c, data, 4u, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack)
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*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, elemsize, elempack, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack)
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*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, h, elemsize, elempack, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack)
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*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, h, c, elemsize, elempack, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack)
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*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, data, elemsize, elempack, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack)
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*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, h, data, elemsize, elempack, (Allocator*)allocator));
}

ncnn_mat_t ncnn_mat_create_external_3d_elem(int w, int h, int c, void* data, size_t elemsize, int elempack)
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*)ncnn_allocator_get_tls_allocator()));
return (ncnn_mat_t)(new Mat(w, h, c, data, elemsize, elempack, (Allocator*)allocator));
}

void ncnn_mat_destroy(ncnn_mat_t mat)
Expand All @@ -200,24 +183,24 @@ void ncnn_mat_fill_float(ncnn_mat_t mat, float v)
((Mat*)mat)->fill(v);
}

ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat)
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*)ncnn_allocator_get_tls_allocator())));
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->clone((Allocator*)allocator)));
}

ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w)
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*)ncnn_allocator_get_tls_allocator())));
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, (Allocator*)allocator)));
}

ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h)
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*)ncnn_allocator_get_tls_allocator())));
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, (Allocator*)allocator)));
}

ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c)
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*)ncnn_allocator_get_tls_allocator())));
return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, c, (Allocator*)allocator)));
}

int ncnn_mat_get_dims(const ncnn_mat_t mat)
Expand Down Expand Up @@ -263,24 +246,24 @@ void* ncnn_mat_get_data(const ncnn_mat_t mat)
#if NCNN_PIXEL

/* mat pixel api */
ncnn_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride)
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*)ncnn_allocator_get_tls_allocator())));
return (ncnn_mat_t)(new Mat(Mat::from_pixels(pixels, type, w, h, stride, (Allocator*)allocator)));
}

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_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*)ncnn_allocator_get_tls_allocator())));
return (ncnn_mat_t)(new Mat(Mat::from_pixels_resize(pixels, type, w, h, stride, target_width, target_height, (Allocator*)allocator)));
}

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_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*)ncnn_allocator_get_tls_allocator())));
return (ncnn_mat_t)(new Mat(Mat::from_pixels_roi(pixels, type, w, h, stride, roix, roiy, roiw, roih, (Allocator*)allocator)));
}

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_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*)ncnn_allocator_get_tls_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)));
}

void ncnn_mat_to_pixels(const ncnn_mat_t mat, unsigned char* pixels, int type, int stride)
Expand Down
44 changes: 20 additions & 24 deletions src/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ void ncnn_allocator_destroy(ncnn_allocator_t allocator);
void* ncnn_allocator_fast_malloc(ncnn_allocator_t allocator, size_t size);
void ncnn_allocator_fast_free(ncnn_allocator_t allocator, void* ptr);

ncnn_allocator_t ncnn_allocator_get_tls_allocator();
void ncnn_allocator_set_tls_allocator(ncnn_allocator_t allocator);

/* option api */
typedef struct __ncnn_option_t* ncnn_option_t;

Expand All @@ -53,27 +50,26 @@ void ncnn_option_set_use_vulkan_compute(ncnn_option_t opt, int use_vulkan_comput
/* mat api */
typedef struct __ncnn_mat_t* ncnn_mat_t;

ncnn_mat_t ncnn_mat_create();
ncnn_mat_t ncnn_mat_create_1d(int w);
ncnn_mat_t ncnn_mat_create_2d(int w, int h);
ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c);
ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data);
ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data);
ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data);
ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack);
ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack);
ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack);
ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack);
ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack);
ncnn_mat_t ncnn_mat_create_external_3d_elem(int w, int h, int c, void* data, size_t elemsize, int elempack);
ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator);
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);
void ncnn_mat_destroy(ncnn_mat_t mat);

void ncnn_mat_fill_float(ncnn_mat_t mat, float v);

ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat);
ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w);
ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h);
ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c);
ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h, ncnn_allocator_t allocator);
ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c, ncnn_allocator_t allocator);

int ncnn_mat_get_dims(const ncnn_mat_t mat);
int ncnn_mat_get_w(const ncnn_mat_t mat);
Expand All @@ -93,10 +89,10 @@ void* ncnn_mat_get_data(const ncnn_mat_t mat);
#define NCNN_MAT_PIXEL_RGBA 4
#define NCNN_MAT_PIXEL_BGRA 5
#define NCNN_MAT_PIXEL_X2Y(X, Y) (X | (Y << 16))
ncnn_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride);
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_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_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_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, ncnn_allocator_t allocator);
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);
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);
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);
void ncnn_mat_to_pixels(const ncnn_mat_t mat, unsigned char* pixels, int type, int stride);
void ncnn_mat_to_pixels_resize(const ncnn_mat_t mat, unsigned char* pixels, int type, int target_width, int target_height, int target_stride);

Expand Down
8 changes: 4 additions & 4 deletions tests/test_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

static int test_c_api_0()
{
ncnn_mat_t a = ncnn_mat_create_1d(2);
ncnn_mat_t b = ncnn_mat_create_1d(2);
ncnn_mat_t a = ncnn_mat_create_1d(2, NULL);
ncnn_mat_t b = ncnn_mat_create_1d(2, NULL);
ncnn_mat_t c = 0;

ncnn_option_t opt = ncnn_option_create();
Expand Down Expand Up @@ -92,8 +92,8 @@ static int test_c_api_1()
20,21,22,23,24,25,26,27
};

ncnn_mat_t a = ncnn_mat_create_external_1d(24, (void*)data);
ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3);
ncnn_mat_t a = ncnn_mat_create_external_1d(24, (void*)data, NULL);
ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, NULL);
ncnn_mat_t c = 0;

ncnn_option_t opt = ncnn_option_create();
Expand Down

0 comments on commit 2b7b92b

Please sign in to comment.