forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PaddlePaddle#56 from Superjomn/refine/runtime-inte…
…rface implement x86 device
- Loading branch information
Showing
5 changed files
with
253 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
set(srcs intrinsic.cc buffer.cc cinn_runtime.cc) | ||
set(srcs intrinsic.cc) | ||
|
||
foreach(cpp ${srcs}) | ||
set(core_src | ||
"${core_src};cinn/runtime/${cpp}" | ||
CACHE INTERNAL "") | ||
endforeach() | ||
|
||
cc_library(cinn_runtime SRCS cinn_runtime.cc buffer.cc cinn_x86_device_impl.cc) | ||
|
||
cc_test(test_cinn_runtime SRCS cinn_runtime_test.cc DEPS cinn_runtime) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "cinn/runtime/cinn_runtime.h" | ||
|
||
extern "C" { | ||
|
||
int cinn_device_malloc(void* context, struct cinn_buffer_t* buf) { | ||
// ASSERT_NOT_NULL(context) | ||
ASSERT_NOT_NULL(buf) | ||
ASSERT_NOT_NULL(buf->device_interface) | ||
return buf->device_interface->impl->malloc(context, buf); | ||
} | ||
|
||
int cinn_device_free(void* context, struct cinn_buffer_t* buf) { | ||
// ASSERT_NOT_NULL(context) | ||
ASSERT_NOT_NULL(buf) | ||
return buf->device_interface->impl->free(context, buf); | ||
} | ||
|
||
int cinn_device_sync(void* context, struct cinn_buffer_t* buf) { | ||
ASSERT_NOT_NULL(buf) | ||
ASSERT_NOT_NULL(buf->device_interface) | ||
// ASSERT_NOT_NULL(context) | ||
buf->device_interface->impl->sync(context, buf); | ||
return 0; | ||
} | ||
|
||
int cinn_device_release(void* context, const struct cinn_device_interface_t* device_interface) { | ||
// ASSERT_NOT_NULL(context) | ||
ASSERT_NOT_NULL(device_interface) | ||
CINN_NOT_IMPLEMENTED | ||
} | ||
|
||
int cinn_copy_to_host(void* context, struct cinn_buffer_t* buf) { | ||
// ASSERT_NOT_NULL(context) | ||
ASSERT_NOT_NULL(buf) | ||
ASSERT_NOT_NULL(buf->device_interface) | ||
return buf->device_interface->impl->copy_to_host(context, buf); | ||
} | ||
|
||
int cinn_copy_to_device(void* context, struct cinn_buffer_t* buf) { | ||
// ASSERT_NOT_NULL(context) | ||
ASSERT_NOT_NULL(buf) | ||
ASSERT_NOT_NULL(buf->device_interface) | ||
return buf->device_interface->impl->copy_to_device(context, buf); | ||
} | ||
int cinn_buffer_copy(void* context, struct cinn_buffer_t* src, struct cinn_buffer_t* dst) { | ||
// ASSERT_NOT_NULL(context); | ||
ASSERT_NOT_NULL(src); | ||
ASSERT_NOT_NULL(dst); | ||
return dst->device_interface->buffer_copy(context, src, dst); | ||
} | ||
|
||
cinn_type_t cinn_int32_t() { return cinn_type_t(cinn_type_int, 32); } | ||
cinn_type_t cinn_int64_t() { return cinn_type_t(cinn_type_int, 64); } | ||
cinn_type_t cinn_uint32_t() { return cinn_type_t(cinn_type_uint, 32); } | ||
cinn_type_t cinn_uint64_t() { return cinn_type_t(cinn_type_uint, 64); } | ||
cinn_type_t cinn_float32_t() { return cinn_type_t(cinn_type_float, 32); } | ||
cinn_type_t cinn_float64_t() { return cinn_type_t(cinn_type_float, 64); } | ||
|
||
} // extern "C" | ||
|
||
struct cinn_buffer_t* cinn_buffer_t::new_(cinn_device_kind_t device, cinn_type_t type) { | ||
struct cinn_buffer_t* x = new (struct cinn_buffer_t); | ||
x->type = type; | ||
x->device = device; | ||
// NOTE set device_interface for each buffer. | ||
switch (x->device) { | ||
case cinn_x86_device: | ||
x->device_interface = &cinn_x86_device_interface; | ||
break; | ||
case cinn_unk_device: | ||
fprintf(stderr, "Device type of buffer should be set, found Unk"); | ||
abort(); | ||
break; | ||
default: | ||
fprintf(stderr, "Not supported device type"); | ||
abort(); | ||
} | ||
|
||
return x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "cinn/runtime/cinn_runtime.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(buffer, basic) { | ||
auto* buffer = cinn_buffer_t::new_(cinn_x86_device, cinn_float32_t()); | ||
ASSERT_TRUE(buffer); | ||
ASSERT_TRUE(buffer->device_interface); | ||
ASSERT_EQ(buffer->device_interface, &cinn_x86_device_interface); | ||
std::vector<cinn_dimension_t> shape({3, 10}); | ||
buffer->resize(shape.data(), shape.size()); | ||
buffer->device_interface->impl->malloc(NULL, buffer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "cinn/runtime/cinn_runtime.h" | ||
|
||
int cinn_x86_malloc(void* context, cinn_buffer_t* buf) { | ||
// ASSERT_NOT_NULL(context) | ||
ASSERT_NOT_NULL(buf) | ||
uint64_t memory_size = buf->num_elements() * buf->type.bytes(); | ||
CINN_CHECK(memory_size > 0); | ||
if (buf->memory_size < memory_size) { | ||
if (buf->host_memory) { | ||
free(buf->host_memory); | ||
} | ||
buf->host_memory = (unsigned char*)malloc(buf->type.bytes() * buf->num_elements()); | ||
buf->memory_size = memory_size; | ||
CINN_LOG("buf.memory size is %d", buf->memory_size); | ||
} | ||
ASSERT_NOT_NULL(buf->host_memory); | ||
return 0; | ||
} | ||
|
||
int cinn_x86_free(void* context, cinn_buffer_t* buf) { | ||
// ASSERT_NOT_NULL(context); | ||
ASSERT_NOT_NULL(buf); | ||
if (buf->host_memory) { | ||
free(buf->host_memory); | ||
buf->host_memory = NULL; | ||
} | ||
return 0; | ||
} | ||
|
||
// All the following operations are not support by X86 device, just leave them empty. | ||
// @{ | ||
int cinn_x86_sync(void* context, cinn_buffer_t* buf) { return 0; } | ||
int cinn_x86_release(void* context) { return 0; } | ||
int cinn_x86_copy_to_host(void* context, cinn_buffer_t* buf) { return 0; } | ||
int cinn_x86_copy_to_device(void* context, cinn_buffer_t* buf) { return 0; } | ||
int cinn_x86_buffer_copy(void* context, cinn_buffer_t* src, cinn_buffer_t* dst) { return 0; } | ||
// @} | ||
|
||
cinn_device_interface_impl_t cinn_x86_device_impl{&cinn_x86_malloc, | ||
&cinn_x86_free, | ||
&cinn_x86_sync, | ||
&cinn_x86_release, | ||
&cinn_x86_copy_to_host, | ||
&cinn_x86_copy_to_device, | ||
&cinn_x86_buffer_copy | ||
|
||
}; | ||
|
||
cinn_device_interface_t cinn_x86_device_interface{&cinn_device_malloc, | ||
&cinn_device_free, | ||
&cinn_device_sync, | ||
&cinn_device_release, | ||
&cinn_copy_to_host, | ||
&cinn_copy_to_device, | ||
&cinn_buffer_copy, | ||
&cinn_x86_device_impl}; |