jna调用main.c封装的capi接口,不稳定复现段错误
Created by: buptwds
模型使用的是线性回归的例子http://paddlepaddle.org/docs/develop/documentation/zh/getstarted/concepts/use_concepts_cn.html 预测样例main.c,https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/capi/examples/model_inference/dense/main.c 把预测样例中的main.c改成infer.c,并生成libpaddle_infer.so,infer.c代码如下:
#include <stdlib.h>
#include <paddle/capi.h>
#include "common.h"
#define MERGE_PATH "models/merge_model"
float infer(float data[], int length) {
// Initalize the PaddlePaddle runtime environment.
char* argv[] = {"--use_gpu=False"};
CHECK(paddle_init(1, (char**)argv));
long size;
void* buf = read_config(MERGE_PATH, &size);
paddle_gradient_machine machine;
CHECK(paddle_gradient_machine_create_for_inference_with_parameters(&machine, buf, size));
paddle_arguments in_args = paddle_arguments_create_none();
CHECK(paddle_arguments_resize(in_args, 1));
paddle_matrix mat =
paddle_matrix_create(/* height = batch size */ 1,
/* width = dimensionality of the data layer */ 2,
/* whether to use GPU */ false);
paddle_real* array;
CHECK(paddle_matrix_get_row(mat, 0, &array));
for(int i = 0; i < length; i++) {
array[i] = data[i];
}
// Assign the matrix to the argument.
CHECK(paddle_arguments_set_value(in_args, 0, mat));
// Create the output argument.
paddle_arguments out_args = paddle_arguments_create_none();
// Invoke the forward computation.
CHECK(paddle_gradient_machine_forward(machine,
in_args,
out_args,
/* is train taks or not */ false));
// Create the matrix to hold the forward result of the neural network.
paddle_matrix prob = paddle_matrix_create_none();
// Access the matrix of the output argument, the predicted result is stored in
CHECK(paddle_arguments_get_value(out_args, 0, prob));
//uint64_t height;
//uint64_t width;
paddle_real* predict = (paddle_real*)malloc(sizeof(paddle_real) * 1);
//CHECK(paddle_matrix_get_shape(prob, &height, &width));
CHECK(paddle_matrix_get_row(prob, 0, &predict));
float result = *predict;
//printf("Prob: \n");
//for (int i = 0; i < height * width; ++i) {
// printf("predict in infer is %.4f ", *predict);
//}
//printf("predict in infer is %.4f\n", *predict);
//cleaning up.
CHECK(paddle_matrix_destroy(prob));
CHECK(paddle_arguments_destroy(out_args));
CHECK(paddle_matrix_destroy(mat));
CHECK(paddle_arguments_destroy(in_args));
CHECK(paddle_gradient_machine_destroy(machine));
return result;
}
生成so的过程如下: gcc -fPIC -c infer.c -o infer.o gcc -shared infer.o -o lib/libpaddle_infer.so -Llib -lpaddle_capi_shared
然后使用java的jna链接libpaddle_infer.so,并调用infer.c中的infer函数,java代码:
package capi;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Main {
public interface Inference extends Library {
Inference INSTANTCE = (Inference) Native.loadLibrary("paddle_infer", Inference.class);
float infer(float sample[], int length);
}
public static void main(String[] args) {
float[] data = {1, 2};
float predict = Inference.INSTANTCE.infer(data, 2);
System.out.println("predict: " + predict);
}
}
不稳定复现以下错误,运行多次才会出现一次(但可以看到,即使出错也能正常打印出predict的值):
I0205 08:43:00.892827 7502 Util.cpp:166] commandline: --use_gpu=False
predict: 1.4857053
*** Aborted at 1517820180 (unix time) try "date -d @1517820180" if you are using GNU date ***
PC: @ 0x0 (unknown)
*** SIGSEGV (@0x7f6d5299b580) received by PID 7501 (TID 0x7f6d0f45d700) from PID 1385805184; stack trace: ***
@ 0x7f6d5256e390 (unknown)
@ 0x7f6d516a8964 check_pending_signals()
@ 0x7f6d5169f823 signal_thread_entry()
@ 0x7f6d517faaa3 JavaThread::thread_main_inner()
@ 0x7f6d517fabec JavaThread::run()
@ 0x7f6d516a9a28 java_start()
@ 0x7f6d525646ba start_thread
@ 0x7f6d51e803dd clone
@ 0x0 (unknown)
Segmentation fault