Skip to content
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

[C-API]需要一个GPU的运行示例 #6389

Closed
peterzhang2029 opened this issue Dec 7, 2017 · 5 comments
Closed

[C-API]需要一个GPU的运行示例 #6389

peterzhang2029 opened this issue Dec 7, 2017 · 5 comments

Comments

@peterzhang2029
Copy link
Contributor

peterzhang2029 commented Dec 7, 2017

目前CAPI中的demo没有直接给出gpu运行的示例,如果将dense目录下的直接改成:

// Initalize Paddle
  char* argv[] = {"--use_gpu=True"};
  CHECK(paddle_init(1, (char**)argv));

  // Reading config binary file. It is generated by `convert_protobin.sh`
  long size;
  void* buf = read_config(CONFIG_BIN, &size);

  // Create a gradient machine for inference.
  paddle_gradient_machine machine;
  CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size));
  CHECK(paddle_gradient_machine_randomize_param(machine));

  // Loading parameter. Uncomment the following line and change the directory.
  // CHECK(paddle_gradient_machine_load_parameter_from_disk(machine,
  //                                                "./some_where_to_params"));
  paddle_arguments in_args = paddle_arguments_create_none();

  // There is only one input of this network.
  CHECK(paddle_arguments_resize(in_args, 1));

  // Create input matrix.
  paddle_matrix mat = paddle_matrix_create(/* sample_num */ 1,
                                           /* size */ 784,
                                           /* useGPU */ true);

运行会出问题:

Prob:
*** Aborted at 1512642274 (unix time) try "date -d @1512642274" if you are using GNU date ***
PC: @                0x0 (unknown)
*** SIGSEGV (@0x33048a1a00) received by PID 17009 (TID 0x7f6c5f37b8c0) from PID 76159488; stack trace: ***
    @     0x7f6c5de65470 (unknown)
    @           0x4017ae main
    @     0x7f6c5de51bd5 __libc_start_main
    @           0x401209 (unknown)
    @                0x0 (unknown)
@Xreki
Copy link
Contributor

Xreki commented Dec 7, 2017

从你的error信息来看,似乎forward已经跑完了,已经打印https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/capi/examples/model_inference/dense/main.c#L62 这一行了。出错的原因应该是https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/capi/examples/model_inference/dense/main.c#L60 获取到的是GPU内存指针,而后面几行试图在CPU上直接访问GPU内存,所以才挂掉的吧。

我在这个PR #5773 里面,有增加了一个GPU的例子,可以参考一下。我们也确实需要一个GPU的例子。

@peterzhang2029
Copy link
Contributor Author

@Xreki 多谢,我的理解是缺少结果从GPU到CPU上的拷贝。

@Xreki
Copy link
Contributor

Xreki commented Dec 7, 2017

对的,较新加入的一个接口https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/capi/Matrix.cpp#L77

PD_API paddle_error paddle_matrix_get_value(paddle_matrix mat, paddle_real* result);

实现了GPU到CPU的拷贝功能,result可以是CPU指针,不过要求提前分配好空间。示例里面使用的paddle_matrix_get_row只是获取GPU内存指针,如果要在CPU端访问其内容,需要显式使用cudaMemcpy将数据传输到GPU上。

@peterzhang2029
Copy link
Contributor Author

@Xreki 明白了。另外,我们在demo里数据输入这边的都是random,感觉如果用gpu的话和实际应用还是有区别的

@peterzhang2029
Copy link
Contributor Author

Closing due to low activity. Feel free to reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants