# Transposing an integer matrix with LibraryLink

Posted 5 years ago
5083 Views
|
7 Replies
|
2 Total Likes
|
 In a previous post I explained how to create and return a matrix with LibraryLink. In this post we are going to expand on this a bit and create a function which accepts an integer matrix and returns the transposed result.Here is the code (call it IntegerMatrixTranspose.c): #include "WolframLibrary.h" DLLEXPORT int IntegerMatrixTranspose(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) { /* Variable declarations */ int err = LIBRARY_NO_ERROR; MTensor in; mint in_type; mint in_rank; mint const *in_dims; mint *in_data; MTensor out; mint out_type; mint out_rank; mint const *out_dims; mint *out_data; int i; mint *tmp_dims; mint rows, row, cols, col; /* Code section */ /* Get the input matrix */ in = MArgument_getMTensor(Args); in_type = libData->MTensor_getType(in); in_rank = libData->MTensor_getRank(in); in_dims = libData->MTensor_getDimensions(in); in_data = libData->MTensor_getIntegerData(in); /* The type and rank of the output matrix is the same as the input matrix */ out_type = in_type; out_rank = in_rank; /* The output dimensions are the reverse of the input dimensions */ tmp_dims = (mint *) malloc(in_rank * sizeof(mint)); for(i=0; iMTensor_new(out_type, out_rank, out_dims, &out); out_data = libData->MTensor_getIntegerData(out); /* Set the elements of the output matrix */ rows = in_dims; cols = in_dims; for(row=0; row Answer
7 Replies
Sort By:
Posted 5 years ago
 I've used MathLink to send data back and forth across LibraryLink. What are the pros and cons of doing that? Answer
Posted 5 years ago
 Both are equivalent in their end results. I would expect a little bit more communication overhead when using MathLink (WSTP), but I don't have any data in hand to back this claim up. Answer
Posted 5 years ago
 I did some benchmarks for this recently: In short: for an empty function call LibraryLink passing is ~5 times faster than MathLink passing. for cycling a large array it's 25 times faster Keep in mind that: these numbers are specific to the precise passing method I used and my own computer. Check the full post to see what it is precisely that was benchmarked. There are many variations on how this can be done. Answer
Posted 5 years ago
 I thought I had to use MathLink to do callbacks to Mathematica through LibraryLink. Is that correct? Answer
Posted 5 years ago
 I think so, but it's not something I personally have a lot of experience with (yet). Answer
Posted 5 years ago
 Not necessarily. Check the section titled "Library Callback Functions" in the LibraryLink docs and ConnectLibraryCallbackFunction. This is much more limited than MathLink and only supports compiled functions. I have never used it, but I would expect it to also be much faster than MathLink. Answer
Posted 5 years ago
 This is a great post for educational purposes, but it should be pointed out that it is (very likely, I haven't tried) going to be significantly slower than the builtin Transpose`.The reason is that naive implementations of simple operations like transposition, array addition, etc. will not be as fast as an explicitly vectorized version. Writing it to take advantage of SIMD operations (like SSE) and considering cache effects should speed it up considerably. The builtin Transpose is certainly written this way.The point I want to come to is that it would be great if LibraryLink provided access to some of these operations, in particular transposition. Mathematica stores matrices in row-major order (for very good reasons). Most other libraries use column-major order. If we want to transfer data between the two, it becomes necessary to transpose while copying. In a future version I would love to see a transpose-copy function for this purpose. Answer