-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Labels
area-native-interopUsed for native interop related issues, including FFI.Used for native interop related issues, including FFI.library-ffitype-questionA question about expected behavior or functionalityA question about expected behavior or functionality
Description
The underlying layer uses C+Openmp multithreading to process function callback calculations. How to link through Dart ffi
Following is my C-lang code
void testOmpCanRun(int row, int column, double ** data, void (*func)(int, int, double ** data)){
// The data has been allocated memory before.
#pragma omp parallel for collapse(2)
for (int r = 0;r < row;r++) {
for (int c = 0;c < column;c ++) {
func(r, c, data);
}
}
}
Following is my Dart-lang code
typedef testOmpCanRun__base__ffi = Void Function(
Int32 row,
Int32 column,
Pointer<Pointer<Double>> data,
Pointer<NativeFunction<Void Function(Int32, Int32, Pointer<Pointer<Double>> data)>> func
);
typedef testOmpCanRun__base = void Function(
int row,
int column,
Pointer<Pointer<Double>> data,
Pointer<NativeFunction<Void Function(Int32, Int32, Pointer<Pointer<Double>> data)>> func
);
late final testOmpCanRun__base matply__testOmpCanRun = dylib.lookupFunction<testOmpCanRun__base__ffi, testOmpCanRun__base>('testOmpCanRun');
// Question core
// Using NativeCallable<T>.listener
// MatrixType is a Matrix-like data-structure,main-component is like as follwed -
/*
- int row;
- int column;
- Pointer<Pointer<Double>> data;
* */
typedef CallBackUp = Void Function(Int32, Int32, Pointer<Pointer<Double>>);
MatrixType justTestOmpCanRun({
required int row,
required int column,
required double Function(int, int) func,
}) {
assert(row > 0 && column > 0);
Pointer<Pointer<Double>> data = matply__allocateButNoNumbers(row, column); // Just allocate memory
final NativeCallable<CallBackUp> callback = NativeCallable<CallBackUp>.listener((int r, int c, Pointer<Pointer<Double>> data){
data[r][c] = func(r, c); // Wrap it as a void function according to the documentation
});
matply__testOmpCanRun(row, column, data, callback.nativeFunction);
callback.close();
return MatrixType.__fromDataPointer(data, [row, column]);
}
Test && Output
double f(int r, int c){
print("Call");
print(r - c);
return 3;
}
main() {
final mt = justTestOmpCanRun(row: 2, column: 8, func: f);
mt.visible();
freeMp(visible: true, hex: false); // just free memory.
}
/*output
[
[0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 ]
[0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 ]
]
Free Memory in location : 2535398893120
*/
As the results are shown , Matrix datas are not changed, keeping initial all-zeros.
Due to the lack of cases and information, I don't konw how to connect openmp multithreaded callback function to Dart ffi (single thread has been solved, and openmp multithreaded non-function callback test was successful).
Metadata
Metadata
Assignees
Labels
area-native-interopUsed for native interop related issues, including FFI.Used for native interop related issues, including FFI.library-ffitype-questionA question about expected behavior or functionalityA question about expected behavior or functionality