Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Cocoa <[email protected]>
  • Loading branch information
cocoa-xu committed Jun 8, 2024
1 parent c5ac902 commit 245703f
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions c_src/modules/evision_mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
#else
#include <unistd.h>
#endif
#if EVISION_HAVE_CUDA
#include <cuda_runtime.h>
#endif

#include "../nif_utils.hpp"
#include "evision_mat_utils.hpp"
#include "evision_backend/backend.h"
#include "evision_mat_api.h"
#include "evision_cuda.h"

using namespace evision::nif;

Expand All @@ -38,24 +37,6 @@ static ERL_NIF_TERM evision_cv_mat_empty(ErlNifEnv *env, int argc, const ERL_NIF
return _evision_make_mat_resource_into_map(env, *res->val, ret);
}

std::pair<std::vector<unsigned char>, int> get_cuda_ipc_handle(std::uintptr_t ptr) {
#ifndef EVISION_HAVE_CUDA
return std::make_pair({}, true);
#else
cudaIpcMemHandle_t ipc_handle;
cudaError_t status = cudaIpcGetMemHandle(&ipc_handle, reinterpret_cast<void*>(ptr));

// Assuming sizeof(cudaIpcMemHandle_t) is constant
const size_t size = sizeof(cudaIpcMemHandle_t);

// Copy the memory handle to a byte array
std::vector<unsigned char> result(size);
memcpy(result.data(), &ipc_handle, size);

return std::make_pair(result, status != cudaSuccess);
#endif
}

// @evision c: mat_to_pointer,evision_cv_mat_to_pointer,2
// @evision nif: def mat_to_pointer(_self,_opts \\ []), do: :erlang.nif_error("Mat::to_pointer not loaded")
static ERL_NIF_TERM evision_cv_mat_to_pointer(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
Expand Down Expand Up @@ -84,13 +65,15 @@ static ERL_NIF_TERM evision_cv_mat_to_pointer(ErlNifEnv *env, int argc, const ER
pointer_vec.push_back(bytePtr[i]);
}
}
else if (pointer_kind == "cuda_ipc") {
auto result = get_cuda_ipc_handle(ptr);
if (result.second) {
return evision::nif::error(env, "Unable to get cuda IPC handle");
}
pointer_vec = result.first;
}
// todo: use nvcc to compile `evision_cuda.{cc,h}`
//
// else if (pointer_kind == "cuda_ipc") {
// auto result = get_cuda_ipc_handle(ptr);
// if (result.second) {
// return evision::nif::error(env, "Unable to get cuda IPC handle");
// }
// pointer_vec = result.first;
// }
if (pointer_vec.size() == 0) {
return enif_make_badarg(env);
}
Expand Down

0 comments on commit 245703f

Please sign in to comment.