Error using GNU scientific library with Code::Blocks - c

I am having an extraordinary problem with my current Code::Blocks (GNU GCC compiler) setup. The compiler seems to selectively run some GSL functions, but seems for some reason to have great problems when commanded to execute other GSL functions.
For example, I have lifted the following code from the following destination:
https://www.gnu.org/software/gsl/manual/html_node/Example-programs-for-matrices.html
I assume that because the code is derived from the official GNU website, that the code is correct:
#include <math.h>
#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
int
main (void)
{
size_t i,j;
gsl_matrix *m = gsl_matrix_alloc (10, 10);
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
gsl_matrix_set (m, i, j, sin (i) + cos (j));
for (j = 0; j < 10; j++)
{
gsl_vector_view column = gsl_matrix_column (m, j);
double d;
d = gsl_blas_dnrm2 (&column.vector);
printf ("matrix column %d, norm = %g\n", j, d);
}
gsl_matrix_free (m);
return 0;
}
From debugging, I have learned that the source of the error is the following line:
d = gsl_blas_dnrm2 (&column.vector);
The compiler crashes at this point and prints the following error message:
Process returned -1073741819 <0xC0000005>
I have spent a lot of time trying to discover the source of the bug but have sadly not had much success. I am generally not sure why there is a crash at all. The debugger prints no warnings or error messages.

I'm going to suggest that perhaps you have mismatched headers and libraries. Perhaps there is more than one version of GSL installed. You have the headers from one version referenced in your source, and the linker is referencing the libs from other version.
I went looking up the typedef of the gsl_vector_view and ended up here, and you may be even able to discern that this version doesn't even support the vector member of that struct type.
You will get this 0xC0000005 error, typically when you use some unitialised pointer. Its not that your pointer is uninitialised here though .. I'd say what's happening is the 'vector' bit of &column.vector is being interpreted as something other than what is intended.
In summary, I think this is some kind of environmental issue, perhaps with your linker settings? There are some details on how to configure these here: http://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/
Hope this helps.

Related

Error: Cannot convert 'char(*)[a]' to 'char(*)[2]'. I don't understand why there's an error

In the code below if instead of a I use a number it works fine. But if i use a it gives the following error:
Cannot convert 'char(*)[a]' to 'char(*)[2]' for argument '2' to 'void displayNumbers(int, char(*)[2])'
#include <stdio.h>
void displayNumbers(int,char num[2][2]);
int main()
{
int a=2;
char num[a][a];// if i write 2 instead of a, it works fine!
displayNumbers(a,num);
return 0;
}
void displayNumbers(int a,char num[2][2])
{
printf("%c\n",a+ num[1][1]);
}
Why does using a or 2 make a difference here? I have a feeling the reason may be trivial but it would be really great if someone helps. The IDE i am using is Dev C++.
You have three primary problems.
First the declaration of displayNumber hard-codes num[2][2] defeating the purpose of using a Variable Length Array (the variable part of the name is at issue). While num[2][2] is valid, it is better written as num[a][a], or best (num*)[a] (a pointer to an array of char [a])
Next, num is completely uninitialized. What do you expect to print? Attempt to read an uninitialized value invokes Undefined Behavior.
Last, your format string in printf ("%c\n", a + num[1][1]); is suspect. The function name is displayNumber, yet you are attempting to print a character. Further, if the value is below ' ' (space, e.g. 0x20, 32), nothing will print, you would be in the Non-Printiable range. If you want to print the number, use the "%d" format specifier.
Putting it altogether, you could do something similar to:
#include <stdio.h>
void displayNumbers (int a, char (*num)[a]);
int main (void) {
int a=2;
char num[a][a];
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++)
num[i][j] = i + j;
displayNumbers (a, num);
return 0;
}
void displayNumbers (int a, char (*num)[a])
{
printf ("%d\n", a + num[1][1]);
}
Example Use/Output
$ ./bin/dispnum
4
Look things over and let me know if you have further questions.
Example Compile and Use on TDM-GCC 4.9.2
I have TDM-GCC 4.9.2 on a Win7 box. It works fine. Here is the version, compile and run on Windows 7:
C:\Users\david\Documents\dev\src-c\tmp>gcc --version
gcc (tdm-1) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
C:\Users\david\Documents\dev\src-c\tmp>gcc -Wall -Wextra -pedantic -Ofast -std=gnu11 -o bin\dispnum dispnum.c
C:\Users\david\Documents\dev\src-c\tmp>bin\dispnum
4
(I left the full directory and path information as an example that you can compile from anywhere as long as gcc.exe is in your path)

What does the "install" function mean in C syntax?

I'm trying to understand the C code called by the R package ucminf. The following code is from the file interface.c found at https://cran.r-project.org/src/contrib/ucminf_1.1-4.tar.gz:
#include <R.h>
#include <Rinternals.h> //R internal structures
#include <R_ext/RS.h> //F77_CALL etc.
// Declare FORTRAN routine for use in C
extern void F77_NAME(ucminf)(int*, double[], double*, double[],
int*,double[],int*,int*,int*,double[],SEXP);
/*-------------------------------------------------------------------------------
Define C functions that calls user defined function in R
*/
void installPar(int nn, double x[], SEXP rho) {
int i;
SEXP PAR = findVarInFrame(rho, install(".x"));
double *xpt = REAL(PAR);
if (LENGTH(PAR) != nn)
error("Dimension mismatch, length(.x) = %d != n = $d", LENGTH(PAR), nn);
for (i = 0; i < nn; i++) xpt[i] = x[i] ;
}
rho is an environment created in R, and it contains the vector .x. My best guess is that the line SEXP PAR = findVarInFrame(rho, install(".x")); is setting PAR equal to .x, but what does the install() command do?
This is such a simple question that I was surprised I couldn't find the answer online - searching for "install c syntax" turned up lots of information about how to install compilers, but I couldn't find anything about the command. Any suggestions for keywords to make my searches more effective would be appreciated.
This code is part of an R extension I think, and therefore the use of install here is a function call to the R C API for Writing Extension. What this does is create the symbol .x in the current symbol table (or return a reference to the existing .x symbol). The linked document does indicate that using install is harmless if the symbol already exists, and is a good way of looking up the symbol if that's what you actually want to do.

MKL BLAS functions not behaving as expected

I can't get Intel MKL to work as it should from C.
I have the following test program:
#include "stdafx.h"
#include"mkl.h"
int main()
{
int one = 1;
int ten = 10;
double copy[10];
double threes[10];
for (int i = 0; i < 10; ++i) threes[i] = 3;
dcopy(&ten, threes, &one, copy, &one);
double l1norm;
l1norm = dasum(&ten, threes, &one);
return 0;
}
which is building and linking fine but not doing what I intended. Specifically at the return line the array "copy" continues to be full of what was there when it was declared and l1norm equal to 0.
I am linking to the libraries : mkl_blas95_ilp64.lib, mkl_core_dll.lib, mkl_intel_ilp64_dll.lib and mkl_intel_thread_dll.lib.
I'm also getting similar problems when running third-party code that calls MKL so I assume the problem is how I have the build configured (in Visual Studio 2015).
The equivalent Fortran program runs fine.
Please check the libraries you link when porting from Fortran to C/C++. MKL requires different libraries and compiling flags with different compilers and settings. At least mkl_blas95_ilp64.lib is not required with C compiler.
Also ILP64 is not common compared to the default model LP64.
MKL Link Line Advisor is a tool provided by Intel to solve this issue. You could use it to check if your libraries and compiling flags are correct.
https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor

Why does this SIMD example code in C compile with minGW but the executable doesn't run on my windows machine?

I'm learning the basics of SIMD so I was given a simple code snippet to see the principle at work with SSE and SSE2.
I recently installed minGW to compile C code in windows with gcc instead of using the visual studio compiler.
The objective of the example is to add two floats and then multiply by a third one.
The headers included are the following (which I guess are used to be able to use the SSE intrinsics):
#include <time.h>
#include <stdio.h>
#include <xmmintrin.h>
#include <pmmintrin.h>
#include <time.h>
#include <sys/time.h> // for timing
Then I have a function to check what time it is, to compare time between calculations:
double now(){
struct timeval t; double f_t;
gettimeofday(&t, NULL);
f_t = t.tv_usec; f_t = f_t/1000000.0; f_t +=t.tv_sec;
return f_t;
}
The function to do the calculation in the "scalar" sense is the following:
void run_scalar(){
unsigned int i;
for( i = 0; i < N; i++ ){
rs[i] = (a[i]+b[i])*c[i];
}
}
Here is the code for the sse2 function:
void run_sse2(){
unsigned int i;
__m128 *mm_a = (__m128 *)a;
__m128 *mm_b = (__m128 *)b;
__m128 *mm_c = (__m128 *)c;
__m128 *mm_r = (__m128 *)rv;
for( i = 0; i <N/4; i++)
mm_r[i] = _mm_mul_ps(_mm_add_ps(mm_a[i],mm_b[i]),mm_c[i]);
}
The vectors are defined the following way (N is the size of the vectors and it is defined elsewhere) and a function init() is called to initialize them:
float a[N] __attribute__((aligned(16)));
float b[N] __attribute__((aligned(16)));
float c[N] __attribute__((aligned(16)));
float rs[N] __attribute__((aligned(16)));
float rv[N] __attribute__((aligned(16)));
void init(){
unsigned int i;
for( i = 0; i < N; i++ ){
a[i] = (float)rand () / RAND_MAX / N;
b[i] = (float)rand () / RAND_MAX / N;
c[i] = (float)rand () / RAND_MAX / N;
}
}
Finally here is the main that calls the functions and prints the results and computing time.
int main(){
double t;
init();
t = now();
run_scalar();
t = now()-t;
printf("S = %10.9f Temps du code scalaire : %f seconde(s)\n",1e5*sum(rs),t);
t = now();
run_sse2();
t = now()-t;
printf("S = %10.9f Temps du code vectoriel 2: %f seconde(s)\n",1e5*sum(rv),t);
}
For sum reason if I compile this code with a command line of "gcc -o vec vectorial.c -msse -msse2 -msse3" or "mingw32-gcc -o vec vectorial.c -msse -msse2 -msse3"" it compiles without any problems, but for some reason I can't run it in my windows machine, in the command prompt I get an "access denied" and a big message appears on the screen saying "This app can't run on your PC, to find a version for your PC, check with the software publisher".
I don't really understand what is going on, neither do I have much experience with MinGW or C (just an introductory course to C++ done on Linux machines). I've tried playing around with different headers because I thought maybe I was targeting a different processor than the one on my PC but couldn't solve the issue. Most of the info I found was confusing.
Can someone help me understand what is going on? Is it a problem in the minGW configuration that is compiling in targeting a Linux platform? Is it something in the code that doesn't have the equivalent in windows?
I'm trying to run it on a 64 bit Windows 8.1 pc
Edit: Tried the configuration suggested in the site linked below. The output remains the same.
If I try to run through MSYS I get a "Bad File number"
If I try to run throught the command prompt I get Access is Denied.
I'm guessing there's some sort of bug arising from permissions. Tried turning off the antivirus and User Account control but still no luck.
Any ideas?
There is nothing wrong with your code, besides, you did not provide the definition of sum() or N which is, however, not a problem. The switches -msse -msse2 appear to be not required.
I was able to compile and run your code on Linux (Ubuntu x86_64, compiled with gcc 4.8.2 and 4.6.3, on Atom D2700 and AMD Athlon LE-1640) and Windows7/64 (compiled with gcc 4.5.3 (32bit) and 4.8.2 (64bit), on Core i3-4330 and Core i7-4960X). It was running without problem.
Are you sure your CPU supports the required instructions? What exactly was the error code you got? Which MinGW configuration did you use? Out of curiosity, I used the one available at http://win-builds.org/download.html which was very straight-forward.
However, using the optimization flag -O3 created the best result -- with the scalar loop! Also useful are -m64 -mtune=native -s.

Generating an output while linking to math library

this is my first time posting to this forum so if I neglect any formalities or bother anyone in any way with something similar please let me know and I'll do my best to avoid doing it again!
I am trying to generate two arrays using the following code, but I am having a problem actually outputting the code. I know this is probably something very basic, but I just started using C only about two weeks ago. I'm quite sure everything is correct, but for some reason when I try to compile the code while linking the math library with gcc static.c -lm -o static and then when I further ask for the output using ./static, I am given no output and am asked for the next command. What am I doing wrong?
If this is something simple or an oversight then the odds are I am missing something conceptually and if you wouldn't mind elaborating on it I would really appreciate it. thank you!
#include <stdio.h>
#include <stdlib.h>
#define N 100
#define pi 3.14
#include <math.h>
int main (void) {
double x[N], f[N];
int i;
for (i = 0; i < N; i++) {
x[i] = (double)(i) / ((double)(N) - 1.0);
f[i] = sin(2.0 * pi * x[i]);
}
return EXIT_SUCCESS;
printf("x is %f",x[i]);
}
You already return success and the code below will not be executed. Swap the two statements.
printf("x is %f",x[i]);
return EXIT_SUCCESS;
Your printf is after the return, so your code doesn't actually reach it. You need to have it before.

Resources