-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a c-api interface to initialize the thread environment of Paddle #5773
Conversation
…nd add a GPU example.
862b3b6
to
3948801
Compare
paddle/capi/Main.cpp
Outdated
if (isInit) return kPD_NO_ERROR; | ||
|
||
if (FLAGS_use_gpu) { | ||
hl_init(FLAGS_gpu_id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hl_init will set the t_resource.device to -1, is it a bug?
paddle/capi/Main.cpp
Outdated
@@ -43,4 +43,16 @@ paddle_error paddle_init(int argc, char** argv) { | |||
isInit = true; | |||
return kPD_NO_ERROR; | |||
} | |||
|
|||
paddle_error paddle_init_thread() { | |||
static __thread bool isInit = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not need 48 and 49 lines.
In the hl_init
interface will determine whether it has been initialized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
@@ -43,4 +43,16 @@ paddle_error paddle_init(int argc, char** argv) { | |||
isInit = true; | |||
return kPD_NO_ERROR; | |||
} | |||
|
|||
paddle_error paddle_init_thread() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to add an argument of device_id
.
Users may want to initialize the thread to a different device environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If adding a device_id, another question is when user invokes the paddle_matrix_create API, which gpu is the matrix on? There is no device id in the paddle_matrix_create API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe need to wrap the hl_get_device
interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to add device_id
and set it to different values. The binary failed:
F1120 08:05:09.430253 1282 hl_cuda_device.cc:565] Check failed: cudaSuccess == cudaStat (0 vs. 77) Cuda Error: an illegal memory access was encountered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This problem is due to the thread share the same parameter model with the main thread. In this case, you need to pass in the same device id as the main thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I know what your means. In the current mode, the Parameters
are shared among multi-threads, but each thread has its own network
. If each thread wants to run on different GPU, it should have its own network
+ Parameters
. Then Parameters
cannot be shared anymore, and we should not use the interface paddle_gradient_machine_create_shared_param
but directly using paddle_gradient_machine_create_for_inference
and paddle_gradient_machine_load_parameter_from_disk
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 我认为,这个多线程的例子,是希望使用
paddle_gradient_machine_create_shared_param
共享参数,不能支持跑在不同的GPU上。 - 如果希望多个线程跑在不同的GPU上,则应该使用
trainer_count>1
或者,在每个线程里面单独地paddle_gradient_machine_create_for_inference
以及paddle_gradient_machine_load_parameter_from_disk
。
paddle/capi/error.h
Outdated
@@ -27,4 +27,21 @@ typedef enum { | |||
kPD_UNDEFINED_ERROR = -1, | |||
} paddle_error; | |||
|
|||
static const char* paddle_error_string(paddle_error err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the function implementation into a .cc file and avoid generating duplicate copies of code in the inference library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
paddle/capi/main.h
Outdated
/** | ||
* Initialize the thread environment of Paddle. | ||
*/ | ||
PD_API paddle_error paddle_init_thread(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since some previous CPU users did not use this interface. Here need to indicate only GPU need this interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
…addle_error_string into a .cpp file.
#include <pthread.h> | ||
#include <time.h> | ||
#include "../common/common.h" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add some comments about this example. For example, this is an inference implementation where multiple threads share a GPU.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
1d78cf9
to
68f6b80
Compare
paddle_init_thread()
need to be called in a newly launched thread to initialize the thread environment of Paddle