Octave: mkoctfile not found when compiling a C++ file - c++

I am trying to use Octave functions in C++. I install Octave-3.8.0 on Mac OS X 10.9.3 and follow the standalone program example on Octave website,
#include <iostream>
#include <octave/oct.h>
int
main (void)
{
std::cout << "Hello Octave world!\n";
int n = 2;
Matrix a_matrix = Matrix (n, n);
for (octave_idx_type i = 0; i < n; i++)
for (octave_idx_type j = 0; j < n; j++)
a_matrix(i,j) = (i + 1) * 10 + (j + 1);
std::cout << a_matrix;
return 0;
}
Then I type
$ mkoctfile --link-stand-alone main.cpp -o standalone
But it shows mkoctfile: command not found. What is the problem?
I also tried to compile the C++ file with g++
$ g++ -I /usr/local/octave/3.8.0/include/octave-3.8.0 main.cpp
but it shows 2 errors as follows.
1) 'config.h' file not found with include; use "quotes" instead.
2) fatal error: 'hdft.h' file not found.
Please help me!

It may be that octave is not registered in your system properly, judging by the shell response.
Try invoking the command from inside the octave interpreter.

Not sure about on MacOS, but on Linux, mkoctfile is not bundled within the default Octave distribution. Instead, it requires a supplementary package, liboctave-dev, that has to be installed in addition to Octave itself.
This is not documented in the Octave web tutorial.

Related

Following c++ code works in leetcode but not in my vscode, why? [duplicate]

This question already has an answer here:
G++ Compiler warning when using c++ 17 updates
(1 answer)
Closed 1 year ago.
I am currently working on a leetcode question, and try to track down the code process in my end, this is my solution:
#include <iostream>
#include <vector>
#include <stack>
#include <utility>
using namespace std;
vector<int> direction{-1, 0, 1, 0, -1};
int maxAreaOfIsland(vector<vector<int>>&grid){
int m = grid.size(), n = m ? grid[0].size() : 0, local_area, area = 0, x, y;
for (int i = 0; i < m; ++i){
for (int j = 0; j < n; ++j){
if(grid[i][j]){
local_area = 1;
grid[i][j] = 0;
stack<pair<int, int>> island;
island.push({i, j});
while(!island.empty()){
auto [r, c] = island.top(); \\problem line, vscode can't understand it
island.pop();
for (int k = 0; k < 4; ++k){
x = r + direction[k], y = c + direction[k + 1];
if(x>=0 && x<m && y>=0 && y<n && grid[x][y]==1){
grid[x][y] = 0;
++local_area;
island.push({x, y});
}
}
}
area = max(area, local_area);
}
}
}
return area;
}
this code works on the leetcode side, but not mine, here is the warning
[Running] cd "c:\Users\chen1\OneDrive\Desktop\C_C++tut\" && g++ leetcode695.cpp -o leetcode695 && "c:\Users\chen1\OneDrive\Desktop\C_C++tut\"leetcode695
leetcode695.cpp: In function 'int maxAreaOfIsland(std::vector<std::vector<int> >&)':
leetcode695.cpp:23:16: warning: structured bindings only available with -std=c++17 or -std=gnu++17
auto [r, c] = island.top();
^
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
[Done] exited with code=1 in 0.932 seconds
Can someone explains why, although I get an alternative way to replace it, it is still annoying and perplexing
thanks for helping
additionally!!!
I actually have my main function; the problem here is a syntax error where leetcode's compiler recognizes it, but not g++, the line that causes the problem is auto [r, c] = island.top();, if I alter it to
int r = get<0>(island.top());
int c = get<1>(island.top());
then it works fine, I just don't understand why leetcode compiler can understand it, but not g++
The linker error (not the warning) is what is causing the build to fail (scroll sideways!):
[...]crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
occurs in MinGW gcc when your code lacks either a main() or WinMain() entry point. As yours does. I guess leetcode (which I have never heard of or used) provides a test harness for you to run the function? It compiles - the message is a linker error, so it cannot form an executable - you need a main().
With respect to the warning, again you need to scroll to the end of the message:
leetcode695.cpp:23:16: warning: structured bindings only available
with -std=c++17 or -std=gnu++17
auto [r, c] = island.top();
can be resolved by specifying C++17 (or higher) compilation (or not using structured bindings). A Windows/VSCode specific solution is discussed at G++ Compiler warning when using c++ 17 updates, but fundamentally it is about setting the compiler switch -std=c++17.

cuda <<<X,X>>> gives expected an expression error

I am trying to compile and run the following program called test.cu:
#include <iostream>
#include <math.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
// Kernel function to add the elements of two arrays
__global__
void add(int n, float* x, float* y)
{
int index = threadIdx.x;
int stride = blockDim.x;
for (int i = index; i < n; i += stride)
y[i] = x[i] + y[i];
}
int main(void)
{
int N = 1 << 20;
float* x, * y;
// Allocate Unified Memory – accessible from CPU or GPU
cudaMallocManaged(&x, N * sizeof(float));
cudaMallocManaged(&y, N * sizeof(float));
// initialize x and y arrays on the host
for (int i = 0; i < N; i++) {
x[i] = 2.0f;
y[i] = 1.0f;
}
// Run kernel on 1M elements on the GPU
add <<<1, 256>>> (N, x, y);
// Wait for GPU to finish before accessing on host
cudaDeviceSynchronize();
// Check for errors (all values should be 3.0f)
for (int i = 0; i < 10; i++)
std::cout << y[i] << std::endl;
// Free memory
cudaFree(x);
cudaFree(y);
return 0;
}
I am using visual studio comunity 2019 and it marks the "add <<<1, 256>>> (N, x, y);" line as having an expected an expression error. I tried compiling it and somehow it compiles without mistakes, but when running the .exe file it outputs a bunch of "1" instead of the expected "3".
I also tried compiling using "nvcc test.cu", but initially it said "nvcc fatal : Cannot find compiler 'cl.exe' in PATH", so i added "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\Hostx64\x64" to path and now compiling with nvcc gives the same mistake as compiling with visual studio.
In both cases the program never enter the "add" function.
I am pretty sure the code is right and the problem has something to do with the installation, but i already tried reinstalling cuda toolkit and repairing MCVS, but it didn't work.
The kernel.cu exemple that appears when starting a new project with cuda in visual studio also didn't work. When running it outputted "No kernel image available for execution on the device".
How can is solve this?
nvcc version if that helps:
nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Wed_Jul_22_19:09:35_Pacific_Daylight_Time_2020
Cuda compilation tools, release 11.0, V11.0.221
Build cuda_11.0_bu.relgpu_drvr445TC445_37.28845127_0
Visual Studio provides IntelliSense for C++. In the C++ language, the proper parsing of angle brackets is troublesome. You've got < as less than and for templates, and << as shift. So, the fact is that the guys at NVIDIA choose the worst possible delimiter <<<>>>. This makes Intellisense difficult to work properly. The way to get full IntelliSense in CUDA is to switch from the Runtime API to the Driver API. The C++ is just C++, and the CUDA is still (sort of) C++, there is no <<<>>> badness for the language parsing to have to work around.
You could take a look at the difference between matrixMul and matrixMulDrv. The <<<>>> syntax is handled by the compiler essentially just spitting out code that calls the Driver API calls. You'll link to cuda.lib not cudart.lib, and may have to deal with a "mixed mode" program if you use CUDA-RT only libraries. You could refer to this link for more information.
Also, this link tells how to add Intellisense for CUDA in VS.

Compiling C++ program using H5Cpp.h header file

I'm need to create a HDF5 file. I'm using the H5Cpp.h header file.
I'm trying to compile the C++ code below on OSX 10.11 El Capitan.
#include "include/hdf5-1.10.0-patch1/c++/src/H5Cpp.h"
using namespace H5;
const int NX = 5;
const int NY = 5;
const H5std_string FILE_NAME( "SDS.h5" );
const H5std_string DATASET_NAME( "IntArray" );
int main(){
int i, j;
int data[NX][NY]; // buffer for data to write
for (j = 0; j < NX; j++){
for (i = 0; i < NY; i++)
data[j][i] = i + j;
}
H5File file(FILE_NAME, H5F_ACC_TRUNC);
hsize_t dimsf[2] = {NX, NY};
DataSpace dataspace(2, dimsf);
DataSet dataset = file.createDataSet(DATASET_NAME, PredType::NATIVE_INT,
dataspace);
// Attempt to write data to HDF5 file
dataset.write(data, PredType::NATIVE_DOUBLE);
return 0;
}
I keep getting this error,
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
Here's the verbose output - https://gist.github.com/gkarthik/e21d2f83baffc2d2eb1b883696c44df8
Thanks!
A few tips:
I think that you have to compile the HDF5 library yourself if you haven't the pre-built binaries. At the HDF5 website there are no pre-built binaries for Mac (as far as I can see).
In windows, to compile a C++ application to use the HDF5 library you have to indicate to the linker the following dependencies (in this order), maybe in mac is also imperative:
szip.lib zlib.lib hdf5.lib hdf5_cpp.lib
If you download some pre-built binaries, there is a file that will indicate the steps to compile your C++ program. In windows it is called: "USING_HDF5_CMake.txt" and "USING_HDF5_VS.txt". You can find these files for example here.

How to get source line number from .ll file LLVM

I am working with LLVM 3.4 and want to obtain the line number information of source file from IR. The IR is generated from simple c code with Clang. I want to obtain the line number in source c file from the line in IR body.
I tried this -
For Instruction BI,
unsigned Line = Line = BI->getDebugLoc().getLine();
For Loop L, std::cout << L->getStartLoc().getLine();
But, the result stored/printed is always 0. I don't know how to obtain line number in the source from LLVM IR.
My Source C file is -
#include <stdio.h>
int main()
{
int i;
int inbuf[100];
int outbuf[100];
for(i = 0; i < 100; ++i)
inbuf[i] ^= outbuf[i];
inbuf[1] += 402;
inbuf[6] += 107;
inbuf[97] += 231;
for(i = 0; i < 100; ++i)
{
inbuf[i] += outbuf[i];
}
inbuf[47] += 312;
//print-statements
for (i=0;i<100;i++) {
printf("inbuf[%d] = %d\n",i,inbuf[i]);
}
return 0;
Command Used-
~/llvm/build/Release+Asserts/bin/clang -O3 -fno-unroll-loops -fno-vectorize -fno-slp-vectorize -S -emit-llvm sample.c -o sample.ll
Thanks!
To get line number information into .ll file your must specify both -O0 and -g flags for clang.
http://llvm.org/docs/SourceLevelDebugging.html#debugging-optimized-code
Line numbers are stored in specialized metadata nodes.
http://llvm.org/docs/LangRef.html#specialized-metadata-nodes
So the full command line must look like this:
~/llvm/build/Release+Asserts/bin/clang -O0 -g -S -emit-llvm sample.c -o sample.ll

Exception throw when using Armadillo and qpOASES

I have a quadratic programming optimization problem that I am solving with qpOASES. In there exists a matrix X that I need to precondition, so I am using Armadillo and the routine arma::pinv from there in order to calculate the Moor-Penrose pseudoinverse.
The problem: I write the matrix X in a file , and then I read it in a separate program (say test.cpp) that does not depend in any way to qpOASES. The routine pinv runs fine.
#include <iostream>
#include <fstream>
#include <armadillo>
#include <string>
using namespace std;
using namespace arma;
int main(){
// Read design matrix.
int NRows = 199;
int NFields = 26;
string flname_in = "chol_out_2_data";
mat A (NRows,NFields);
for (int i=0; i < NRows; ++i)
for (int j=0; j < NFields; ++j)
myin >> A(i,j) ;
// Calculate pseudoinverse
mat M;
pinv(M,A); // <========= THIS fails when I use flag: -lqpOASES
}
When I include the same routine in the file where I perform the QP optimization (say true_QP.cpp), I get a runtime error, due to pinv not being able to calculate the pseudo inverse. I've done extensive tests, the file is read in OK and the values are the same.
I've tracked down the problem that is a conflict in the following way: I compiled the program that does not depend in any way on qpOASES (test.cpp - as described above) also with the flag -lqpOASES and then, the code gives run time error.
That is,compile:
g++ test.cpp -o test.xxx -larmadillo
runs fine:
./test.xxx
compile:
g++ test.cpp -o test.xxx -larmadillo -lqpOASES
throws exception (due to failure of calculating pinv):
./test.xxx
Therefore I suspect some conflict - it seems that using -lqpOASES affects some flag in armadillo also? Any ideas? Is there some dependency in LAPACK/BLAS or some flag internally that may change the setup of Armadillo? Thank you for your time.
Here is the documentation for the arma::pinv function:
http://arma.sourceforge.net/docs.html#pinv
I have resolved the issue by calculating pinv from Eigen, instead of Armadillo.
The function definition I used for Eigen, based on this bug report:
http://eigen.tuxfamily.org/bz/show_bug.cgi?id=257
is:
template<typename _Matrix_Type_>
Eigen::MatrixXd pinv(const _Matrix_Type_ &a, double epsilon =std::numeric_limits<double>::epsilon())
{
Eigen::JacobiSVD< _Matrix_Type_ > svd(a ,Eigen::ComputeThinU | Eigen::ComputeThinV);
double tolerance = epsilon * std::max(a.cols(), a.rows()) *svd.singularValues().array().abs()(0);
return
svd.matrixV() * (svd.singularValues().array().abs() > tolerance).select(svd.singularValues().array().inverse(), 0).matrix().asDiagonal() * svd.matrixU().adjoint();
}