Armadillo: eigs_gen can not successfully give answer - sparse-matrix

I'm using armadillo's eigs_gen to find the smallest algebraic eigenvalue of a sparse matrix, but eigs_gen can not give an answer. I test the eigs_gen for eye matrix 6*6, it also can not give the answer. The code is:
#include <iostream>
#include <armadillo>
using namespace arma;
using namespace std;
int main(int argc, char** argv)
{
cout << "Armadillo version: " << arma_version::as_string() << endl;
sp_mat A = speye<sp_mat>(6, 6);
A.print("A:");
cx_vec eigval;
cx_mat eigvec;
eigs_gen(eigval, eigvec, A, 1, "sm", 1); // find 1 eigenvalues/eigenvectors
eigval.print("eigval:");
return 0;
}
The answer is:
Armadillo version: 6.100.0 (Midnight Blue)
A:
[matrix size: 6x6; n_nonzero: 6; density: 16.67%]
(0, 0) 1.0000
(1, 1) 1.0000
(2, 2) 1.0000
(3, 3) 1.0000
(4, 4) 1.0000
(5, 5) 1.0000
*** Error in `./test': double free or corruption (out): 0x00007fff38dd6910 ***
Aborted (core dumped)
However, an correct answer could be achieved, when the eye matrix is 5*5.
The code is:
#include <iostream>
#include <armadillo>
using namespace arma;
using namespace std;
int main(int argc, char** argv)
{
cout << "Armadillo version: " << arma_version::as_string() << endl;
sp_mat A = speye<sp_mat>(5, 5);
A.print("A:");
cx_vec eigval;
cx_mat eigvec;
eigs_gen(eigval, eigvec, A, 1, "sm", 1); // find 1 eigenvalues/eigenvectors
eigval.print("eigval:");
return 0;
}
the answer is:
Armadillo version: 6.100.0 (Midnight Blue)
A:
[matrix size: 5x5; n_nonzero: 5; density: 20.00%]
(0, 0) 1.0000
(1, 1) 1.0000
(2, 2) 1.0000
(3, 3) 1.000
(4, 4) 1.0000
eigval:
(+1.000e+00,+0.000e+00)
My compile command is:
g++ test.cpp -o test -O2 -I/usr/local/include/armadillo -L/usr/local/lib -DARMA_DONT_USE_WRAPPER -larpack -llapack -lblas -lf2c -lgfortran
I'm working on ubuntu 14.04LTS.

I have find the solution to this problem. The reason is that libopenblas, liblapack, libarpack were compiled by myself and all were Packaged as a static library. When they were used together, this problem was happened.
The solution is that install these packages through commands listed below.
sudo apt-get install libopenblas-dev
sudo apt-get install liblapack-dev
sudo apt-get install libarpack2-dev

Related

How do I compile a cilk program?

I installed Cilk using the instructions from their website.
sudo apt-add-repository ppa:wsmoses/tapir-toolchain
sudo apt-get update
sudo apt-get install tapirclang-5.0 libcilkrts5
I copied the following program from the Cilk documentation.
#include <stdio.h>
#include <stdint.h>
int64_t fib(int64_t n) {
if (n < 2) return n;
int x, y;
x = cilk_spawn fib(n - 1);
y = fib(n - 2);
cilk_sync;
return x + y;
}
int main(){
printf("%ld\n", fib(20));
}
I then compiled using the compiler flag that they specified.
clang-5.0 -fcilkplus Fib.c
Fib.c:7:9: error: use of undeclared identifier 'cilk_spawn'
x = cilk_spawn fib(n - 1);
^
Fib.c:9:5: error: use of undeclared identifier 'cilk_sync'
cilk_sync;
^
The desired output is a working executable that uses Cilk and prints 6765.
What magic incantations are needed to produce this executable?
I am running Ubuntu 18.04 with kernel 4.4.0-45-generic.

OSX 10.12 GCC OpenMP Error

Have been trying to compile and run a simple OpenMP program (Hello World) on OSX 10.12. I installed gcc 6 using brew.
I have been building using the '-fopenmp' flag.
The program compiles fine, but when I try to execute the program I get the following:
dyld: lazy symbol binding failed: Symbol not found: ___emutls_get_address
Referenced from: /usr/local/opt/gcc/lib/gcc/6/libgomp.1.dylib (which was built for Mac OS X 10.12)
Expected in: /usr/lib/libSystem.B.dylib
dyld: Symbol not found: ___emutls_get_address
Referenced from: /usr/local/opt/gcc/lib/gcc/6/libgomp.1.dylib (which was built for Mac OS X 10.12)
Expected in: /usr/lib/libSystem.B.dylib
Abort trap: 6
Any ideas?
Here's an example that runs on my iMac with g++-6, compiled as follows:
g++-6 -std=c++11 -fopenmp -O3 demo.cpp -o demo
Run as follows:
./demo
Time: 4.132ms, 1000000 elements.
Code as follows:
#include "omp.h"
#include <iostream>
#include <cmath>
#include <cstdio>
#include <chrono>
int main()
{
const int size = 1000000;
int i;
double sinTable[size];
std::chrono::time_point<std::chrono::high_resolution_clock> start = std::chrono::high_resolution_clock::now();
#pragma omp parallel for
for(int n=0; n<size; ++n)
sinTable[n] = sqrt(std::sin(2 * M_PI * n / size));
std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now();
std::uint64_t duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();
float ms=duration/1000000.0;
std::cout << "Time: " << ms << "ms, " << size << " elements." << std::endl;
}

Cuda returns wrong value from __device__ function when cos is used

I'm trying to run this code on nvidia GPU and it returns strange values. It consist of two modules main.cu and exmodul.cu (listed bellow). For building I'm using:
nvcc -dc -arch sm_35 main.cu
nvcc -dc -arch sm_35 exmodul.cu
nvcc -arch sm_35 -lcudart -o main main.o exmodul.o
If I run that I obtained strange last line!!! gd must be 1.
result=0
result=0
result=0
result=0
gd=-0.5
When I change 1.0 in exmodul.cu to number greater than
1.000000953 or bellow 0.999999999999999945, it return proper
result.
When I change 1.1 in exmodul.cu it also fails except value 1.0.
Behavior doesn't depend on constant 2.0 in the same module.
When I use another function instead of cos like sin or exp it works properly.
Use of double q = cos(1.1); has no effect.
When I copy function extFunc() to module main.cu it works properly.
If I uncomment *gd=1.0; in main.cu it returns correct 1.0.
Tested on Nvidia GT750M and GeForce GTX TITAN Black. (on GT750M returns different value gd=6.1232329394368592e-17 but still wrong). OS: Debian Jessie.
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2013 NVIDIA Corporation
Built on Thu_Mar_13_11:58:58_PDT_2014
Cuda compilation tools, release 6.0, V6.0.1
Have you got any idea what is wrong?
Thanks, Lukas
main.cu
#include <stdio.h> // printf
#include "exmodul.h" // extFunc
__global__ void mykernel(double*gd);
void deviceCheck();
int main(int argc, char *argv[])
{
double gd, *d_gd;
cudaMalloc(&d_gd, sizeof(double)); deviceCheck();
mykernel<<<1,1>>>(d_gd); deviceCheck();
cudaMemcpy(&gd, d_gd, sizeof(double), cudaMemcpyDeviceToHost);
deviceCheck();
cudaFree(d_gd); deviceCheck();
fprintf(stderr,"gd=%.17g\n",gd);
return 0;
}
void deviceCheck()
{
cudaError_t result = cudaSuccess;
cudaDeviceSynchronize();
result = cudaGetLastError();
fprintf(stderr,"result=%d\n",result); fflush(stderr);
}
__global__ void mykernel(double *gd)
{
*gd = extFunc();
//*gd=1.0;
__syncthreads();
return;
}
exmodul.cu
#include "exmodul.h"
__device__ double extFunc()
{
double q = 1.1;
q = cos(q);
if(q<2.0) { q = 1.0; }
return q;
}
exmodul.h
__device__ double extFunc();
I was able to reproduce problematic behavior on a supported config (CUDA 6.5, CentOS 6.2, K40).
When I switched from CUDA 6.5 to CUDA 7 RC, the problem went away.
The problem also did not appear to be reproducible on an older config (CUDA 5.5, CentOS 6.2, M2070)
I suggest switching to CUDA 7 RC to address this issue. I suspect an underlying bug in the compilation process that has been fixed already.

Compile and run a C bit operation file

I am working on this bitwise operators in C program.i need to compile and run the program to check the output is working correct or not . what compiler i need to use to run and execute the program and what is the command to execute this C program:
enter code here
#include <stdio.h>
#include "bitslab.h"
int main()
int bitAnd(int x, int y)
int getByte(int x, int n)
int logicalShift(int x, int n)
{
printf("bitAnd Result: %d\n", bitAnd(15,3));
printf("getByte Result: %d\n", getByte(20,4));
printf("logicalShift Result: %d\n", logicalShift(12,4));
return ~((~x)|(~y));
return (x>>(n<<3))&0xff;
return ((x >> n) & ((1 << ((~n + 1) + 32)) + ~0));
}
On linux flavours use GCC to compile your program.
http://www.wikihow.com/Compile-a-C-Program-Using-the-GNU-Compiler-(GCC)
On windows you can try turbo-C or visual C++.
I guess your program is not complete and I fear it would not run properly.
Enjoy debugging and happy coding.

CMake Noob segmentation fault issue

I'm trying to learn CMake using an example project that calculates the fibonacci of a given number. My project originally included a single ".c" file and header. I was able to build with CMake and run without issue. Now I'm trying to learn how to link libraries by moving my fibnoacci function into a separate ".c" file which I compile into a linkable library using CMake. It builds without issue but throws a segmentation fault when I run it. My project structure is:
fib
|
*---MathFunctions
| |
| *----CMakeLists.txt
| |
| *----myfib.h
|
*---CMakeLists.txt
|
*---fib.c
|
*---fib.h
|
*---myfib.c
|
*---Config.in.h
The CMakeLists.txt under the MathFunctions folder is empty. All of the program logic is in fib.c and myfib.c. All of the build is in the top CMakeLists.txt
fib.c:
# include "stdio.h"
# include "stdlib.h"
# include "Config.h"
#include "myfib.h"
void internalfib(int num)
{
printf("Internally defined fib\n");
int a, b;
a = 0;
b = 1;
printf( "custom fib of %d", b );
for( int i = 0; i + a <= num; b = i ) {
i = a + b;
a = b;
printf( ", %d", i );
}
}
int main( int argc, char** argv) {
fprintf(stdout,"%s Version %d.%d\n",
argv[0],
VERSION_MAJOR,
VERSION_MINOR);
#ifdef SHOW_OWNER
fprintf(stdout, "Project Owner: %s\n", OWNER);
#endif
myfib(atof( argv[1] ));
printf("\n");
return EXIT_SUCCESS;
}
myfib.c:
# include "stdio.h"
# include "stdlib.h"
void myfib(int num)
{
printf("custom myfib");
int a, b;
a = 0;
b = 1;
printf( "custom fib of %d", b );
for( int i = 0; i + a <= num; b = i ) {
i = a + b;
a = b;
printf( ", %d", i );
}
}
CMakeLists.txt:
#Specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.6)
#Name your project here
project(fibonacci)
enable_testing()
set (VERSION_MAJOR 1)
set (VERSION_MINOR 0)
configure_file (
"${PROJECT_SOURCE_DIR}/Config.h.in"
"${PROJECT_BINARY_DIR}/Config.h"
)
option (SHOW_OWNER "Show the name of the project owner" ON)
#Sends the -std=c99 flag to the gcc compiler
add_definitions(-std=c99)
include_directories("${PROJECT_BINARY_DIR}")
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
add_library(MathFunctions myfib.c)
#This tells CMake to fib.c and name it fibonacci
add_executable(fibonacci fib.c)
target_link_libraries (fibonacci MathFunctions)
#test that fibonacci runs
add_test (FibonacciRuns fibonacci 5)
#Test the fibonacci of 5
add_test (FibonacciCompare5 fibonacci 5)
set_tests_properties (FibonacciCompare5 PROPERTIES PASS_REGULAR_EXPRESSION "1, 1, 2, 3, 5")
install (TARGETS fibonacci DESTINATION ${PROJECT_BINARY_DIR}/bin)
After running "..cmake" and "make" from the build folder I run:
~/dev/cworkshop/fib/build$ ./fibonacci
./fibonacci Version 1.0
Project Owner: Clifton C. Craig
Segmentation fault: 11
Where am I going wrong?
Are you actually sure you're getting a segfault because of your build system?
Your code requires input, and you do not check that input is given. From fib.c:26:
myfib(atof( argv[1] ));
If no arguments are given, your code will try to read data that you haven't provided (and segfault). If you run your code: ./fibonacci 3, you'll print myfib. I'm going to guess this is what your problem is.
Other things to consider:
when giving a portion of code, try to ensure that it's either minimal and complete, or accurately describes your problem. Large blocks of commented-out code are not relevant to the question, while in this case header files and Config.h.in (which are required to get your example to compile), are not provided.
myfib.c and myfib.h are in different folders. If myfib.c is part of the MathFunctions library, I'd put the sources into that subdirectory. This is explained in the tutorial I think you're following: this one, which has a CMakeLists.txt file containing the line:
add_library(MathFunctions mysqrt.cxx)
in the MathFunctions library.
As pointed out by #n.m in the comments, atof takes a float as input - the Fibonacci sequence is generated using an int (so atoi is more appropriate).
For your example, I would not use add_definitions to set the compiler flags. The better way to do it (project-wide) is to use set ( CMAKE_CXX_FLAGS "-O0 -g -Wall"). (There are lots of useful CMake Variables). While add_definitions works, it's not the best tool for the job you're doing. :)

Resources