Matrix and vector multiplication problem with vecLib infrastructure on Mac OS X 10.7

I just started using the vecLib framework to make a program that performs intensive matrix vector multiplications on Mac OS X 10.7. I made a simple such program; Multiply the matrix a by the vector x and add the result by the vector y.

#include <vecLib/vectorOps.h> #include <stdio.h> float a[8][4] = // the matrix to be multiplied { {1.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f, 1.0f}, {1.0f, 0.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, }; float x[4] = {1.0f, 2.0f, 4.0f, 8.0f}; // the vector to be multiplied float y[8] = {0.f, 0.f, 0.f, 0.f, // the result vector 0.f, 0.f, 0.f, 0.f}; int main() { int i; vSgemv('n', 8, 4, 1.0f, (const vFloat *)a, (const vFloat *)x, 1.0f, (vFloat *)y); for (i = 0; i < 8; i++) { printf("%.4f\n", y[i]); } return 0; } 

I compiled and ran the program on the console

 gcc -framework vecLib -o test test.c && ./test 

But the result was this: the operation was not performed, and the result vector remained empty.

 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 

Am I missing some initialization to run matrix and vector functions in vecLib?

+4
source share
1 answer

First, the real mistake is very simple, but you might not know; you pass 'n' for the first argument, but you really need to pass 'n' (despite what is written in the header). With this fix, your code works.

Now, having said that you are doing a couple of more subtle things โ€œwrongโ€ (ish).

First, do not use vecLib. It has been replaced by Accelerate.framework (at 10.4!). vecLib.framework is only supported for older versions. Any new development should refer to Accelerate.

Secondly, do not use the v * functions defined in vectorOps.h . They have also been replaced by the standard BLAS functions defined in cblas.h . Because they are standard, there are many public documents on how to use them, and they are also supported by much faster implementations; vectorOps functions are only supported for legacy support. cblas.h also supports many more operations and data types. If this was not enough, if you decide to port your code to iOS, you will find that the vectorOps functions are not available at all. Use the cblas.h functions.

Re-write your example:

 #include <Accelerate/Accelerate.h> #include <stdio.h> float a[8][4] = // the matrix to be multiplied { {1.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f, 1.0f}, {1.0f, 0.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, }; float x[4] = {1.0f, 2.0f, 4.0f, 8.0f}; // the vector to be multiplied float y[8] = {0.f, 0.f, 0.f, 0.f, // the result vector 0.f, 0.f, 0.f, 0.f}; int main() { int i; cblas_sgemv(CblasRowMajor, CblasNoTrans, 8, 4, 1.0f, (float*)a, 4, x, 1, 1.0f, y, 1); for (i = 0; i < 8; i++) { printf("%.4f\n", y[i]); } return 0; } 

and its launch gives:

 scanon$ gcc test.c -framework Accelerate -o test scanon$ ./test 1.0000 2.0000 3.0000 12.0000 5.0000 5.0000 7.0000 8.0000 
+10
source

All Articles