Compiling lapacke example code? - c++

I'm trying to compile the examples found here:
http://www.netlib.org/lapack/lapacke.html#_examples
Specifically, I'm trying to get the "Calling CGEQRF and the CBLAS" example to work. The code is like so:
#include <stdio.h>
#include <stdlib.h>
#include <lapacke.h>
#include <cblas.h>
int main (int argc, const char * argv[])
{
lapack_complex_float *a,*tau,*r,one,zero;
lapack_int info,m,n,lda;
int i,j;
float err=0.0;
m = 10; n = 5; lda = m;
one = lapack_make_complex_float(1.0,0.0);
zero= lapack_make_complex_float(0.0,0.0);
a = calloc(m*n,sizeof(lapack_complex_float));
r = calloc(n*n,sizeof(lapack_complex_float));
tau = calloc(m,sizeof(lapack_complex_float));
for(j=0;j<n;j++)
for(i=0;i<m;i++)
a[i+j*m] = lapack_make_complex_float(i+1,j+1);
info = LAPACKE_cgeqrf(LAPACK_COL_MAJOR,m,n,a,lda,tau);
info = LAPACKE_cungqr(LAPACK_COL_MAJOR,m,n,n,a,lda,tau);
for(j=0;j<n;j++)
for(i=0;i<n;i++)
r[i+j*n]=(i==j)?-one:zero;
cblas_cgemm(CblasColMajor,CblasConjTrans,CblasNoTrans,
n,n,m,&one,a,lda,a,lda,&one,r,n );
for(i=0;i<n;i++)
for(j=0;j<n;j++)
err=MAX(err,cabs(r[i+j*n]));
printf("error=%e\n",err);
free(tau);
free(r);
free(a);
return(info);
}
If I save the file as a .cpp (perhaps this is my first mistake?) and compile using
g++ lapacketest.cpp -llapack
I get the following compile errors:
3_20_2.cpp:14:7: error: assigning to '_Complex float *' from incompatible type 'void *'
a = calloc(m*n,sizeof(lapack_complex_float));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3_20_2.cpp:15:7: error: assigning to '_Complex float *' from incompatible type 'void *'
r = calloc(n*n,sizeof(lapack_complex_float));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3_20_2.cpp:16:9: error: assigning to '_Complex float *' from incompatible type 'void *'
tau = calloc(m,sizeof(lapack_complex_float));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3_20_2.cpp:29:25: error: use of undeclared identifier 'cabs'
err=MAX(err,cabs(r[i+j*n]));
I tried changing to .c and compiling with gcc, but I got other strange errors. Any thoughts? I'm slowly trying transition from Matlab to c++ for scientific computing and so far it's just been a giant headache.

It looks like calloc(m*n,sizeof(lapack_complex_float)); is not returning a pointer to an object of type lapack_complex_float.

I could compile it but you have to change the line
#include <lapacke.h>
to
#include <lapacke_utils.h>
You find at http://www.netlib.org/lapack/explore-html/da/d8e/lapacke__utils_8h_source.html. After you download it and put in the same directory of your program or use the tags -L -I to locate the library on your system during the compilation.
To compile use:
gcc CGEQRF_CUNGQR.c -llapacke -lblas -lm

Related

Compile errors when calling GNU Octave from C++

I'm trying to write a function that embeds the Octave interpreter in C++, as described here https://octave.org/doc/v4.0.1/Standalone-Programs.html .
I'm trying to do this from a program that I'm writing in Eclipse, and trying to compile with GCC on Linux. I want to be able to call an external script, as in the second example in the link.
My code so far looks like this.....
#include <iostream>
#include <oct.h>
#include <octave.h>
#include <parse.h>
#include <interpreter.h>
using namespace std;
class OctaveInt {
public:
void callOctave (double, int, string);
OctaveInt(string path );
private:
octave::interpreter interpreter;
};
// Member functions including constructor..
OctaveInt::OctaveInt(string path)
{
// Constructor - initialises engine and sets path
int status = interpreter.execute();
octave_value_list p;
p(0) = path;
octave_value_list o1 = octave::feval ("addpath", p);
}
void OctaveInt::callOctave(double params, int size, string name) {
std::cout << "Hello World" << std::endl;
int n = 2;
octave_value_list in;
octave_value_list p;
for (octave_idx_type i=0; i < size; i++)
in(i) = octave_value(params[i]);
octave_value_list out = octave::feval (name, in);
std::cout << "Output is ";
std::cout << out(0).int_value();
}
int main() {
double params[] = {100, 2, 3, 4, 5, 6};
int size = 6;
string path = "/home/arwel/eclipseWorkspace_new/octaveCaller/src/";
OctaveInt octI(path);
octI.callOctave(params, size, "myFunction");
return 0;
}
When I try to compile however, I get a series of errors.....
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/include/octave-5.2.0/octave/ -I/usr/share/octave/5.2.0/etc/tests -I/usr/lib/x86_64-redhat-linux6E -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/octaveCaller.d" -MT"src/octaveCaller.o" -o "src/octaveCaller.o" "../src/octaveCaller.cpp"
../src/octaveCaller.cpp: In constructor ‘OctaveInt::OctaveInt(std::string)’:
../src/octaveCaller.cpp:31:6: warning: unused variable ‘status’ [-Wunused-variable]
int status = interpreter.execute();
^
../src/octaveCaller.cpp: In member function ‘void OctaveInt::callOctave(double, int, std::string)’:
../src/octaveCaller.cpp:48:32: error: invalid types ‘double[octave_idx_type {aka long int}]’ for array subscript
in(i) = octave_value(params[i]);
^
../src/octaveCaller.cpp:42:6: warning: unused variable ‘n’ [-Wunused-variable]
int n = 2;
^
../src/octaveCaller.cpp: In function ‘int main()’:
../src/octaveCaller.cpp:65:44: error: no matching function for call to ‘OctaveInt::callOctave(double [6], int&, const char [11])’
octI.callOctave(params, size, "myFunction");
^
../src/octaveCaller.cpp:65:44: note: candidate is:
../src/octaveCaller.cpp:38:6: note: void OctaveInt::callOctave(double, int, std::string)
void OctaveInt::callOctave(double params, int size, string name) {
^
../src/octaveCaller.cpp:38:6: note: no known conversion for argument 1 from ‘double [6]’ to ‘double’
make: *** [src/octaveCaller.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.
12:41:36 Build Failed. 3 errors, 2 warnings. (took 1s.927ms)
So it looks like I have some problems with types of variables (??).
I don't really know much C++, so I'm undoubtedly doing some basic C++ mistake. Can someone give me a hand to figure out what I'm doing wrong?

Warning against C++ implicit conversion

I have this C++ code:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main() {
vector<int64_t> vec = {4294967296, 4294967296, 4294967296};
int64_t sum = accumulate(vec.begin(), vec.end(), 0, [](int64_t sum, int64_t val){
return sum + val;
});
cout << "sum = " << sum << endl;
}
It returns sum = 0 because implicit conversion from int to int64 (see 0 as third argument of accumulate function). After replacing 0 with (int64_t)0 everything works fine.
But can I detect such things in compile time? -Wconversion does not work in this case.
If the code of std::accumulate were not in a system header file, you would get the warning:
int init=0;
init += *vec.begin() //warning: conversion from 'int64_t' {aka 'long int'} to 'int' may change value [-Wconversion]
But many warnings are disabled for system header files because such warning would cause many noisy and irrelevant messages.
This behavior can be reproduced. Suppose you have this file test.hpp:
int64_t i;
int j = i;
If you compile a file test.cpp in the same directory that includes this file:
with c++ -I . test.cpp -Wconversion, the warning message is printed;
with c++ -isystem . test.cpp -Wconversion the warning message is not printed!
This is exactly what happens to header files of the standard library, the include directory is by default specified with -isystem.
The system header warning message suppression can be disabled with option -Wsystem-header
Demo
And it can be seen on the Demo the warning message is hidden within a bunch or irrelevant warning messages.

c++ Using C99 complex with FFTW in C++14

I'm having trouble using the C99 complex data type with FFTW and C++14.
// fftw14.cc
#include <complex.h>
#include <fftw3.h>
int main() {
fftw_complex* in;
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * 100);
in[0] = 1337.42;
return 0;
}
There is no problem when I compile it with g++ fftw14.cc -lfftw3 -lm. However, using C++14 (g++ -std=c++14 fftw14.cc -lfftw3 -lm), fftw_complex gets double[2] and hence the implicit typecast fails.
fftw14.cc: In function ‘int main()’:
fftw14.cc:11:8: error: incompatible types in assignment of ‘double’ to ‘fftw_complex {aka double [2]}’
in[0] = 1337.42;
^
Why is that? I know I could mess around with the std::complex<double> type, but I would like to avoid that.

Get the following errors when I compile with G++

I am a bit new to programming as you can probably tell from my prior question(s). I was wondering if anyone could help me with this recent problem I've had. I am trying to compile a script main.cpp using g++ but I get the following errors:
Donny#Donny-PC /cygdrive/c/Users/Donny/Desktop/equation/equations/equations
$ g++ main.cpp -o don.exe
main.cpp:3:11: error: ‘::main’ must return ‘int’
void main(){
^
main.cpp: In function ‘int main()’:
main.cpp:36:22: error: ‘pow’ was not declared in this scope
float n=pow(10.0,9.0);
^
main.cpp:43:27: error: ‘sin’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:43:44: error: ‘tan’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:48:40: error: ‘cos’ was not declared in this scope
d*((pow(tan(theta1),2))/cos(theta1)) +
^
The weird thing is that this code works when compiled with microsoft visual studio 2010 C++. Any help would be greatly appreciated!
EDIT:
So, the fixed a lot of the errors shown above, but I am still having a little difficulty fixing the void main error. Here is how my code looks:
#include<iostream>
#include<cmath>
using namespace std;
void main(){
float r, i, f, beta, alpha;
cout<<"Enter value of R : ";............
Any help or examples would be greatly appreciated.
The first error should be self-explanatory. The standard says that the main function must return int but you have declared it as void. Return 0 from your main function to indicate normal termination. The Microsoft compiler is not as strict on this point.
All your remaining errors can be remedied by using #include <math.h>.

c++ compilation : what did i do wrong

I'm new to c++, and I've started a project for my internship where I have use to the Snap library from stanford (http://snap.stanford.edu/). So I have downloaded the library and I am now trying to create my own little programm using it. Sadly i can't seem to be able to compile it :(
Here are the sources :
Makefile :
CXXFLAGS += -std=c++98 -Wall
LDFLAGS += -lrt
Snap.o :
g++ -c $(CXXFLAGS) ../snap/snap/Snap.cpp -I../snap/glib -I../snap/snap -pg
simulation.o : simulation.cpp simulation.h
g++ -g -c $(CXXFLAGS) simulation.cpp
test.o : test.cpp
g++ -g -c $(CXXFLAGS) test.cpp
test : test.o Snap.o simulation.o
g++ -g $(LDFLAGS) test.o Snap.o simulation.o -I../snap/glib -I../snap/snap -lm -o test
simulation.h
#ifndef SIMULATION
#define SIMULATION
#include <vector>
#include "../snap/snap/Snap.h"
class Simulation{
public:
Simulation():score(-1),nNodes(-1),nEdges(-1), dMax(-1){};
Simulation(int nN, int nE, int d);
Simulation(int d, PUNGraph g);
void setDMax(int d){ dMax = d; }
double getScore(){ return score; }
int getNNodes(){ return nNodes; }
int getNEdges(){ return nEdges; }
int getDMax(){ return dMax; }
PUNGraph getGraph(){ return graph; }
std::vector<int> getAlignment(){ return alignment; }
double computeEnergy();
private:
double score;
int nNodes;
int nEdges;
int dMax;
PUNGraph graph;
std::vector<int> alignment;
};
#endif
simulation.cpp
#include "simulation.h"
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include "../snap/snap/Snap.h"
Simulation::Simulation(int nN, int nE, int d){
nNodes = nNodes;
nEdges = nEdges;
dMax = dMax;
graph = TSnap::GenRdnGnm<PUNGraph>(nNodes,nEdges);
for(int i=1; i<=nNodes; i++){
alignment.push_back(i);
}
random_shuffle(alignment.begin(),alignment.begin()+nNodes);
computeEnergy();
}
Simulation::Simulation(int d, PUNGraph g){
nNodes = graph->GetNodes();
nEdges = graph->GetEdges();
dMax = d;
graph = g;
for(int i=1; i<=nNodes; i++){
alignment.push_back(i);
}
random_shuffle(alignment.begin(),alignment.begin()+nNodes);
computeEnergy();
}
double computeEnergy(){
return 0.0;
}
test.cpp
#include<stdlib.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include "simulation.h"
#include "../snap/snap/Snap.h"
int main(int argc, char** argv){
Simulation sim(5000,30000,30);
}
I don't think my compilation problems come from Snap itself and it might very well be only from my poor knowledge of c++ and how the includes an so on are working.
Here is what I get after running make :
g++ -g -c -std=c++98 -Wall simulation.cpp
In file included from /usr/include/c++/4.5/bits/stl_algo.h:61:0,
from /usr/include/c++/4.5/algorithm:63,
from simulation.cpp:5:
/usr/include/c++/4.5/bits/algorithmfwd.h:347:41: error: macro "max" passed 3 arguments, but takes just 2
/usr/include/c++/4.5/bits/algorithmfwd.h:358:41: error: macro "min" passed 3 arguments, but takes just 2
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected initializer before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:347:5: error: template declaration of ‘const _Tp& std::max’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected initializer before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:358:5: error: template declaration of ‘const _Tp& std::min’
In file included from /usr/include/c++/4.5/algorithm:63:0,
from simulation.cpp:5:
/usr/include/c++/4.5/bits/stl_algo.h: In function ‘void std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance)’:
/usr/include/c++/4.5/bits/stl_algo.h:3172:26: error: expected unqualified-id before ‘(’ token
/usr/include/c++/4.5/bits/stl_algo.h: In function ‘void std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance, _Compare)’:
/usr/include/c++/4.5/bits/stl_algo.h:3202:26: error: expected unqualified-id before ‘(’ token
simulation.cpp: In constructor ‘Simulation::Simulation(int, int, int)’:
simulation.cpp:11:13: error: ‘GenRdnGnm’ is not a member of ‘TSnap’
simulation.cpp:11:38: error: expected primary-expression before ‘>’ token
simulation.cpp:11:47: warning: left-hand operand of comma has no effect
I'd be very glad if some one could help resolve my problems (and if you feel like giving some c++/programming wisdom in the process i'd be even happier :) )
Ortholle
The Snap library headers contain the unfortunate macro definitions:
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
This will cause problems with code that uses (or defines) std::min and std::max.
You can get around this by making sure to include STL headers before Snap, or possibly by adding
#undef min
#undef max
after including the Snap.h header.
Another problem with your code: What's with all those extraneous #includes? Example: Your test.cpp #includes a whole bunch of stuff it doesn't need. All that test.cpp needs is (or should need) simulation.h. simulation.cpp has a similar problem with far too many #includes.
Don't #include something in a file that isn't used in that file.
(Aside: that random_shuffle in simulation.cpp should be std::random_shuffle).
None of these fixes are going to help with the base problem, which is that the Snap library 'conveniently' defines max and min as macros. You don't need these, so undef them.