Crate EMCompute

Source
Expand description

fast , simple and cross-platform GPGPU parallel computing library NOTE : there are still some problems with vulkan backend on linux ##Example

  • this example is for v4.0.0 C ABI
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>  
#include "EMCompute.h"
 
int main() {
 Define the kernel
 CKernel kernel;
 kernel.x = 60000;  // Number of workgroups in the x dimension
 kernel.y = 1000;
 kernel.z = 100;

 // WGSL code to perform element-wise addition of example_data and example_data0
 const char* code = 
   "@group(0)@binding(0) var<storage, read_write> v_indices: array<u32>; "
   "@group(0)@binding(1) var<storage, read> v_indices0: array<u32>; "
   "@compute @workgroup_size(10 , 1 , 1)" 
   "fn main(@builtin(global_invocation_id) global_id: vec3<u32>) { "
   "  let idx = global_id.x % 60000; "
   "   "
   "v_indices[idx] = v_indices[idx] + v_indices0[idx]; "
   "  "
   "}";

 uintptr_t index = set_kernel_default_config(&kernel);
 kernel.kernel_code_index = register_computing_kernel_code(index , code , "main");



 // Initialize data
 uint32_t example_data[60000];
 uint32_t example_data0[60000];

 for (int i = 0; i < 60000; ++i) {
   example_data[i] = 1;
   example_data0[i] = 1;
 }

 // Bind data
 DataBinder data;
 data.bind = 0;
 data.data = (uint8_t *)example_data;
 data.data_len = sizeof(uint32_t)*60000/sizeof(uint8_t);

 DataBinder data0;
 data0.bind = 1;
 data0.data = (uint8_t *)example_data0;
 data0.data_len = sizeof(uint32_t)*60000/sizeof(uint8_t);

 DataBinder group0[] = {data, data0};
 GroupOfBinders wrapper;
 wrapper.group = 0;
 wrapper.datas = group0;
 wrapper.datas_len = 2;

 GroupOfBinders groups[] = {wrapper};

 compute(&kernel, groups, 1);
  

 // Check results
 printf("example_data[4]: %d\n", example_data[50000]);
 printf("example_data0[4]: %d\n", example_data0[4]);

 free_compute_cache();

 return 0;
}

Structs§

CKernel
CKernel which will represent your GPU task like how Manifest.xml does in an android project
DataBinder
this struct is for passing data based on its bind index in gpu side
GPUComputingConfig
as config field you have to provide GPUComputingConfig which represent settings which you wanted
GPUCustomSettings
this struct represents custom settings
GPUDeviceInfo
this struct is used for storing information about each device
GPUDevices
this function stores an dynamic array of GPUDeviceInfo with len , it must be freed with free_gpu_devices_infos function after usage
GPUMemoryCustom
with this struct you set min - max of memory you will need in gpu side
GPUSpeedCustom
this struct is used for advance customizations refered as custom_speed settings
GroupOfBinders
all DataBinder types which have the same @group index in your kernel code must all be gathered in this type

Enums§

GPUComputingBackend
computing backends of the api
GPUDeviceType
Computing devices types
GPUMemorySettings
this settings used to tell gpu pre information about our work
GPUPowerSettings
this enum is used to tell to API to setup GPU resources based on power saving rules or not
GPUSpeedSettings
this enum affects speed of the api by setting how much gpu resources are needed directly , if you take too much which your hardware cannot provide , panic happens

Functions§

compute
the simple and compact function for sending your computing task to the gpu side
create_computing_gpu_resources
since v4.0.0 you must create_computing_gpu_resources it will return gpu_res_descriptor as uintptr_t (usize) and you have to pass it as config_index value to CKernel variable
free_compute_cache
since version 2.0.0 api does caching for gpu resources on the memory . the api does deallocate the caches automatically , but in some cases you might want to do it manually so just call free_compute_cache();
free_compute_kernel_codes
when your work fully finished with kernel codes and you wont need to use them anymore , you can use this function to cleanup all the mess which they created from memory
free_gpu_devices_infos
this function is used for deallocating GPUDevices type from C side
get_computing_gpu_infos
this function returns GPUDevices of passed GPUComputingBackend
register_computing_kernel_code
since v4.0.0 your kernel code must be registered before you want to use it . gpu_res_index is gpu resource descriptor which you get from create_computing_gpu_resources .
set_kernel_default_config
because setting CKernel config can be annoying if you just want to do simple task , this function provides general config which will meet most of your needs . since v4.0.0 this function calls create_computing_gpu_resources automatically and assign its return value to config_index of your CKernel variable . only use this function once in your programs , instead of using this many times and causing memory leaks (well all that mem can be freed by free_compute_cache function) use config_index field of CKernel variable