Cannot build C++ code using lapack - c++

I wrote a simple code to test boost and lapack
But it doesn't work well
My code :
#include < iostream>
#include < boost/numeric/bindings/traits/ublas_matrix.hpp>
#include < boost/numeric/ublas/matrix.hpp>
#include < boost/numeric/bindings/lapack/syev.hpp>
#include < boost/numeric/ublas/io.hpp>
namespace ublas = boost::numeric::ublas;
namespace lapack = boost::numeric::bindings::lapack;
int main() {
ublas::matrix<double> A(3,3);
ublas::vector<double> B(3);
A(0,0)=1;
A(1,1)=2;
A(2,2)=4;
A(0,1)=.5;
A(0,2)=.25;
A(1,2)=.3;
lapack::syev('V','L',A,B);
std::cout << A << std::endl;
return 0;
}
My option g++ to build on ubuntu 10.04
g++ -llapack test.cpp
It's error :
no matching function for call to ‘syev(char....... )‘
If there's not "lapack::syev('V','L',A,B);" It's build okay!
Plz Help me !!

Please install the libboost-all-dev package.

Related

gnuplot iostream error "The code executionc annot proceed because boost_iostream.. was not found"

I am attempting to use gnuplot-iostream to run the following code:
#include <iostream>
#include <vector>
#include <random>
#include <numeric>
#include "gnuplot-iostream.h"
int main() {
Gnuplot gp("\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\"");
std::random_device rd;
std::mt19937 mt(rd());
std::normal_distribution<double> normdist(0., 1.);
std::vector<double> v0, v1;
for (int i = 0; i < 1000; i++) {
v0.push_back(normdist(mt));
v1.push_back(normdist(mt));
}
std::partial_sum(v0.begin(), v0.end(), v0.begin());
std::partial_sum(v1.begin(), v1.end(), v1.begin());
gp << "set title 'graph of two random lines'\n";
gp << "plot '-' with lines title v0,"
<< "'-' with lins title 'v1'\n";
gp.send(v0);
gp.send(v1);
std::cin.get();
}
This code produces the following error:
I am currently running the program in c++17 and there are 0 errors in the gnuplot-iostream header. I am unsure of the problem. I have reinstalled the program with no success. Thank you!

Program crashing with C++ DLL using OpenMP

I have a program using OpenMP on C++ and I need it to port into Dll so I can call it from Python. It returns an array of double values, which calculated using a lot of for loops with openmp pragma. I was doubtful if it is going to work, so I started from a little test program that calculates Pi value in a loop with different precision values, then I would measure performance and ensure that OpenMP works properly that way. Plain (w/o Omp) implementation works fine from Python and C++, however Omp variant gives a runtime error in Python (exception: access violation writing 0x000000000000A6C8) and crashes without an error in C++. Also Omp variant works fine if it is not a Dll and just a regular executable. The Dll is made with a makefile. App that uses the Dll built into an executable with g++ with no flags (source code is in UnitMain.cpp). All the relevant code and a Makefile below (I didn't include some files and functions for brevity).
UPD: I tried Microsoft compiler and it works, also I tested a linux dynamic library on WSL/g++ and it also works. Looks like it is Windows gcc specific, I'll try another version of gcc (btw my current version is this):
Thread model: posix gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
UnitFunctions.cpp
#include "UnitFunctions.h"
#include <omp.h>
#include <stdio.h>
#include <string.h>
typedef long long int64_t;
double pi(int64_t n) {
double sum = 0.0;
int64_t sign = 1;
for (int64_t i = 0; i < n; ++i) {
sum += sign/(2.0*i+1.0);
sign *= -1;
}
return 4.0*sum;
}
void calcPiOmp(double* arr, int N) {
int64_t base = 10e5;
#pragma omp parallel for
for(int i = 0; i < N; ++i) {
arr[i] = pi(base+i);
}
}
UnitMain.cpp
#include <windows.h>
#include <iostream>
using namespace std;
struct DllHandle
{
DllHandle(const char * const filename)
: h(LoadLibrary(filename)) {}
~DllHandle() { if (h) FreeLibrary(h); }
const HINSTANCE Get() const { return h; }
private:
HINSTANCE h;
};
int main()
{
const DllHandle h("Functions.DLL");
if (!h.Get())
{
MessageBox(0,"Could not load DLL","UnitCallDll",MB_OK);
return 1;
}
typedef const void (*calcPiOmp_t) (double*, int);
const auto calcPiOmp = reinterpret_cast<calcPiOmp_t>(GetProcAddress(h.Get(), "calcPiOmp"));
double arr[80];
calcPiOmp(arr, 80);
cout << arr[0] << endl;
return 0;
}
Makefile
all: UnitEntryPoint.o UnitFunctions.o
g++ -m64 -fopenmp -s -o Functions.dll UnitEntryPoint.o UnitFunctions.o
UnitEntryPoint.o: UnitEntryPoint.cpp
g++ -m64 -fopenmp -c UnitEntryPoint.cpp
UnitFunctions.o: UnitFunctions.cpp
g++ -m64 -fopenmp -c UnitFunctions.cpp
A Python script
import numpy as np
import ctypes as ct
cpp_fun = ct.CDLL('./Functions.dll')
cpp_fun.calcPiNaive.argtypes = [np.ctypeslib.ndpointer(), ct.c_int]
cpp_fun.calcPiOmp.argtypes = [np.ctypeslib.ndpointer(), ct.c_int]
arrOmp = np.zeros(N).astype('float64')
cpp_fun.calcPiOmp(arrOmp, N)

Creating a Vector of std::thread causes runtime error in g++ but runs fine in Visual Studio?? Why?

I have been stumped for days and my mind has given up on it trying to understand. Looked online for reason/ solution but couldn't find any... not sure why??!??
Using g++ version 11.1.0
Version of VS I am using
Here is the code. Thanks!!!
#include <iostream>
#include <thread>
#include <vector>
const int PRODUCER_COUNT = 10;
void Producer(int index) {
}
int main() {
std::vector<std::thread> producers_and_consumers;
int i = 0;
// Create producers
for (int i = 0; i < PRODUCER_COUNT; ++i)
producers_and_consumers.push_back(std::thread(Producer, i));
// Wait for consumers and producers to finish
for (auto& t : producers_and_consumers)
t.join();
getchar();
return 0;
}

Visual studio code C++(auto keyword error but the code compiler and shows the correct output)

Code when I was testing vectors:
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> a;
for(int i = 0;i<5;i++){
a.push_back(i);
}
for(auto i = a.begin();i!=a.end();++i){
cout<<*i<<" ";
}
}
Now this shows the following error:
Error
But the code compiles correctly
the output
I am not sure what's wrong with the auto keyword.

BigInteger java method to gmp c++

I want to convert java code in c++
code is
BigInteger value = new BigInteger(125, RandomNumber);
BigInteger clone = new BigInteger(value.toByteArray());
How to write this code in cpp using gmp library?
Please anyone help me.
Thanks.
With C++ you can do that
#include <gmpxx.h>
#include <gmp.h>
#include <iostream>
using namespace std;
int main(){
mpz_class value;
mpz_class clone;
gmp_randclass r(gmp_randinit_default);
value = r.get_z_bits(125);
clone = value;
cout << value << endl;
cout << clone << endl;
return 0;
}
and compile with
g++ file.cpp -lgmpxx -lgmp
to install libgmpxx.a
put --enable-cxx to the build option of ./configure
here is a carbon copy from wikipedia
Here is an example of C code showing the use of the GMP library to multiply and print large numbers:
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
int main(void)
{
mpz_t x;
mpz_t y;
mpz_t result;
mpz_init(x);
mpz_init(y);
mpz_init(result);
mpz_set_str(x, "7612058254738945", 10);
mpz_set_str(y, "9263591128439081", 10);
mpz_mul(result, x, y);
gmp_printf("\n %Zd\n*\n %Zd\n--------------------\n%Zd\n\n", x, y, result);
/* free used memory */
mpz_clear(x);
mpz_clear(y);
mpz_clear(result);
return EXIT_SUCCESS;
}
This code calculates the value of 7612058254738945 × 9263591128439081.
Compiling and running this program gives this result. (The -lgmp flag is used if compiling on Unix-type systems.)
7612058254738945
*
9263591128439081
--------------------
70514995317761165008628990709545