mex matlab function c++ gives wrong answer relative to R c++ function
I've got an issue that I cannot get the answer after mex'ing c++ function. It gives zeros always no matter what.
code:
https://github.com/jstriaukas/codequestions/tree/master/code
main.m gives the answer in matlab. linSolver.cpp contains mexFunction that I think I made a mistake in but cannot find it. Any ideas?
See also questions close to this topic
-
omp parallel for - loop execution stuck after 1/6 of iterations have been done (with 6 threads)
I'm having problems parallelizing my code. I'm using C++/CLI with windows forms in VS2017. I was used to just write my parallel loops with concurrency parallel_for but crt is not supoported with /clr, which forces me to use OpenMP. I am a beginner with omp and the research I made didn't give me a solution to my problem. My code runs completely fine without the "#pragma omp parallel for" line. I only want to parallelize the inner loop, because that's where the most computationally expensive part is. Inside the inner loop, I am using some variables from outside both loops, but I only read them, I do not change them. All the other variables are declared inside the inner loop, which should not cause any problems. My code runs untill some time and then just freezes, not computing anything (according to my task manager).
I played around with the loop ranges and found out, that when I change the parallel for loop range, the freeze occurs at a different iteration - I eventually figured it's always at 1/6 of the loop range (my CPU has 6 single-threaded cores). Only one core seems to be computing only 1/6 of the loop. I tried to run some other code with the same "#pragma omp parallel for" syntax to check if my compiler or project settings are right, and with the simple test code, I am really getting 100% utilization, roghly 6x speedup. Any ideas, what might be wrong?
for (int mimosloupeciter = 0; mimosloupeciter < iters_horizontal; mimosloupeciter++)//X cyklus { double beta = asin(abs((double)mimosloupeciter - floor((double)iters_horizontal / 2)) / R); #pragma omp parallel for for (int meridianiter = 0; meridianiter < iters_vertical; meridianiter++)//Y cyklus { //some expensive computation } }
-
Is there is a difference in time and space complexicity between equalization sign and initialisation?
For example i have the following code:
int value = 10; int firstMethod = value; int secondMethod (value);
What is the difference in time and space if I use second method? Is it gonna take longer? Is there any reason why people write code with first method?
-
AWS lambda C++ runtime in SAM
Does anyone have a step-by-step tutorial on using SAM to package an AWS lambda function using the C++ runtime so I can run it locally? C++ is not one of the languages supported using
sam init --runtime
and I cannot work out the steps needed to package the Hello World function from https://aws.amazon.com/blogs/compute/introducing-the-c-lambda-runtime/ -
DiagrammeR export_graph Invalid asm.js
I'm having a problem exporting graphs in
R
to PDFs usingDiagrammeR's
export_graph
function inRStudio
.Example below to reproduce the problem. The PDFs are produced inconsistently so sometimes not at all.
The error message I get is on calling the export_graph in the code snipet below.
I'm using RStudio Version 1.1.463 and R 3.5.2 on Windows 10.
"\<"unknown">":1919791: Invalid asm.js: Function definition doesn't match use"
library(data.tree) library(yaml) library(DiagrammeR) library(DiagrammeRsvg) fileName <- system.file("extdata", "jennylind.yaml", package="data.tree") cat(readChar(fileName, file.info(fileName)$size)) lol <- yaml.load_file(fileName) jl <- as.Node(lol) pic <- ToDiagrammeRGraph(jl) render_graph(pic) export_graph(pic, "C:/Tmp/plot.pdf", file_type = "pdf")
-
Plotting in ggplot after converting to data.frame with a single column?
I'm trying to convert some simple data into a form I thought ggplot2 would accept.
I snag some simple stock data and now I just want to plot, later I want to plot say a 10-day moving average or a 30-day historical volatility period to go with it, which is I'm using ggplot.
I thought it would work something like this line of pseudocode
ggplot(maindata)+geom_line(moving average)+geom_line(30dayvol)
library(quantmod) library(ggplot2) start = as.Date("2008-01-01") end = as.Date("2019-02-13") start tickers = c("AMD") getSymbols(tickers, src = 'yahoo', from = start, to = end) closing_prices = as.data.frame(AMD$AMD.Close) ggplot(closing_prices, aes(y='AMD.Close'))
But I can't even get this to work. The problem of course appears to be that I don't have an x-axis. How do I tell ggplot to use the index column as a. Can this not work? Do I have to create a new "date" or "day" column?
This line for instance using the Regular R plot function works just fine
plot.ts(closing_prices)
This works without requiring me to enter a hard x-axis, and produces a graph, however I haven't figured out how to layer other lines onto this same graph, evidently ggplot is better so I tried that.
Any advice?
-
Using scale_color_gradient2 with a variable of class Date
I'm trying to color by date with ggplot2, but when I try to customize the color using
scale_color_gradient2
, I get an error sayingError in as.Date.numeric(value) : 'origin' must be supplied
.I can't seem to figure out how to pass the origin to
scale_color_gradient2
.I've provided an example below. Any advice?
set.seed(1) x1 <- rnorm(100) x2 <- rnorm(100) day <- sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 100) myData <- data.frame(x1, x2, day) # this plot works as expected ggplot(myData, aes(x = x1, y = x2, color = day)) + geom_point() # scale_color_gradient2() asks for an origin, but I can't figure out how to supply one ggplot(myData, aes(x = x1, y = x2, color = day)) + geom_point() + scale_color_gradient2()
-
Is there a faster way to compute element wise exponentials of a matrix in Matlab?
I am profiling my code and this one line seems to be a bottleneck. The idea is to create a spatial correlation matrix of n observations of a given dimension, based upon a theta vector of the same dimension--which contains dimensional hyperparameters to be fit.
In my overall code this spatial correlation function is called thousands of times (per iteration) while computing a likelihood function in order to optimize my theta parameters. The number of observations grows by one in each iteration of the overall code, and the theta parameters must be refit each iteration. So you can see how this line of code becomes crucial as the algorithm progresses.
I think that the slowest line, i.e.,
R = exp(sum(bsxfun(@times, -abs(D_X).^corr_model,reshape(theta,[1 1 d])),3));
takes the most time to compute the final element-wise exponential across the resulting n by n matrix (the n by n matrix is yielded after summing over the 3 dimension of the distance matrix). But there is a lot going on in this line, so I am unsure if this is the most critical aspect to the overall performance.Thanks for any insight you may have!
I have already replaced a
repmat
command with absxfun
, to multiply the given thetas across the dimensional distance matrixD_X
, which sped up the code notably. However, nesting a secondbsxfun
to execute the squaring of distances i.e., to replaceabs(D_X).^corr_model
, made the code run slower.n = 500; dimension = 20; D_X = repmat(rand(n),[1 1 dimension]); theta = rand(dimension, 1); corr_model = 2; R = corr(corr_model,theta, D_X); function R = corr(corr_model,theta,D_X) % calculate the correlation matrix for the modified nugget effect model % corr_model - the correlation model used for the spatial correlation % corr_model = 2: gaussian correlation function % theta - vector of hyperparameters % D_X - the distance matrix for the input locations d = size(theta,1); switch corr_model case 2 %Gaussian correlation function R = exp(sum(bsxfun(@times, -abs(D_X).^corr_model,reshape(theta,[1 1 d])),3)); end end
-
pinv(H) is not equal to pinv(H'*H)*H'
I'm testing the
y = SinC(x)
function with single hidden layer feedforward neural networks (SLFNs) with 20 neurons.With a SLFN, in the output layer, the output weight(OW) can be described by
OW = pinv(H)*T
after adding regularized parameter
gamma
, whichOW = pinv(I/gamma+H'*H)*H'*T
with
gamma -> Inf, pinv(H'*H)*H'*T == pinv(H)*T, also pinv(H'*H)*H' == pinv(H).
But when I try to calculate
pinv(H'*H)*H'
andpinv(H)
, I find a huge difference between these two when neurons number is over 5 (under 5, they are equal or almost the same).For example, when
H
is10*10
matrix,cond(H) = 21137561386980.3
,rank(H) = 10
,H = [0.736251410036783 0.499731137079796 0.450233920602169 0.296610970576716 0.369359425954153 0.505556211442208 0.502934880027889 0.364904559142718 0.253349959726753 0.298697900877265; 0.724064281864009 0.521667364351399 0.435944895257239 0.337878535128756 0.364906002569385 0.496504064726699 0.492798607017131 0.390656915261343 0.289981152837390 0.307212326718916; 0.711534656474153 0.543520341487420 0.421761457948049 0.381771374416867 0.360475582262355 0.487454209236671 0.482668250979627 0.417033287703137 0.329570921359082 0.315860145366824; 0.698672860220896 0.565207057974387 0.407705930918082 0.427683127210120 0.356068794706095 0.478412571446765 0.472552121296395 0.443893207685379 0.371735862991355 0.324637323886021; 0.685491077062637 0.586647027111176 0.393799811411985 0.474875155650945 0.351686254239637 0.469385056318048 0.462458480695760 0.471085139463084 0.415948455902421 0.333539494486324; 0.672003357663056 0.607763454504209 0.380063647372632 0.522520267708374 0.347328559602877 0.460377531907542 0.452395518357816 0.498449772544129 0.461556360076788 0.342561958147251; 0.658225608290477 0.628484290731116 0.366516925684188 0.569759064961507 0.342996293691614 0.451395814182317 0.442371323528726 0.525823695636816 0.507817005881821 0.351699689941632; 0.644175558300583 0.648743139215935 0.353177974096445 0.615761051907079 0.338690023332811 0.442445652121229 0.432393859824045 0.553043275759248 0.553944175102542 0.360947346089454; 0.629872705346690 0.668479997764613 0.340063877672496 0.659781468051379 0.334410299080102 0.433532713184646 0.422470940392161 0.579948548513999 0.599160649563718 0.370299272759337; 0.615338237874436 0.687641820315375 0.327190410302607 0.701205860709835 0.330157655029498 0.424662569229062 0.412610204098877 0.606386924575225 0.642749594844498 0.379749516620049]; T=[-0.806458764562879 -0.251682808380338 -0.834815868451399 -0.750626822371170 0.877733363571576 1 -0.626938984683970 -0.767558933097629 -0.921811074815239 -1]';
There is a huge difference between
pinv(H'*H)*H*T
andpinv(H)*T
, wherepinv(H'*H)*H*T = [-4803.39093243484 3567.08623820149 668.037919243849 5975.10699147077 1709.31211566970 -1328.53407325092 -1844.57938928594 -22511.9388736373 -2377.63048959478 31688.5125271114]'; pinv(H)*T = [-19780274164.6438 -3619388884.32672 -76363206688.3469 16455234.9229156 -135982025652.153 -93890161354.8417 283696409214.039 193801203.735488 -18829106.6110445 19064848675.0189]'.
I also find that if I round
H
,round(H,2)
,pinv(H'*H)*H*T
andpinv(H)*T
return the same answer. So I guess one of the reason might be the float calculation issue inside the matlab.But since
cond(H)
is large, any small change ofH
may result in large difference in the inverse ofH
. I think theround
function may not be a good option to test. As Cris Luengo mentioned, with largecond
,the numerical imprecision will affect the accuracy of inverse.In my test, I use 1000 training samples with noise between
[-0.2,0.2]
, and test samples are noise free. 20 neurons are selected. TheOW = pinv(H)*T
can give reasonable results forSinC
training, while the performance forOW = pinv(H'*H)*T
is worse. Then I try to increase the precision ofH'*H
bypinv(vpa(H'*H))
, there's no significant improvement.Does anyone know how to solve this?
-
Is there a step-by-step description that enables me to produce a sensor error model for an IMU?
I've noticed sensor error models produced within the following for the IMU used:
1) http://hdl.handle.net/2027.42/58460
2) Ferraris, F., Grimaldi, U., & Parvis, M. (1995). Procedure for effortless in-field calibration of three-axis rate gyros and accelerometers. Sensors and Materials, 7, 311-311.
However, I'm still unclear as to how to create the sensor models for the MEMS IMU for a commercial device. Any resources that help me go through it step-by-step in layman terms?
-
String comparisons in javascript doesn't work
If($(pres).val() == presPRE){console.log('title egal').trim();} Var n = $(pres).val().localCompare(presPRE); Console.log('n='+n);
The console say - 1 but those text are identical. Can you help me?
-
How can I get DataAnnotations.Compare to compare properties from 2 different objects?
I'm getting an error using the view below
The property Models.m_Join.Member.password could not be found.
The 'Password' field that the error refers to is part of the 'member' object that is in the m_Join model.
I've tried using : -
[System.ComponentModel.DataAnnotations.Compare("member.password", ErrorMessageResourceType = typeof(Resources.errors), ErrorMessageResourceName = "passwordsDontMatch")] [System.ComponentModel.DataAnnotations.Compare("member_password", ErrorMessageResourceType = typeof(Resources.errors), ErrorMessageResourceName = "passwordsDontMatch")]
But neither of them work.
public class m_Join { public Member member { get; set; } [System.ComponentModel.DataAnnotations.Compare("password", ErrorMessageResourceType = typeof(Resources.errors), ErrorMessageResourceName = "passwordsDontMatch")] public string passwordConfirm { get; set; } }
What do I have to use to get the code to compare member.password and passwordConfirm
Edit: The 2 properties I want to compare are in different objects. The properties in the proposed duplicate question are in the same object
-
SQL Schema compare software Ubuntu & ms-sql
I'm switching my work PC from Windows to Ubuntu. As I'm responsable to deploy DBs into production and maintain them I'm currently looking for a schema compare software. For now I used RedGate SQLCompare - but that's windows only. Any tools or script suggestions?
-
trying to compile .cu file with mexcuda command on matlab
I have written a cpp function for solving a problem with ordinary differential equations (ODE) on GPU, for this I am using CUDA thrust and boost odeint libraries. I want to use my function on Matlab so I make a .cu file with mex function inside and trying to compile it with mexcuda command on matlab 2018b:
mexcuda mexGpuOde.cu '-IC:\Program Files\boost\boost_1_69_0' Building with 'NVIDIA CUDA Compiler'. Error using mex mexGpuOde.cu
C:/Program Files/boost/boost_1_69_0\boost/core/noncopyable.hpp(42): error: defaulted default constructor cannot be constexpr because the corresponding implicitly declared default constructor would not be constexpr C:/Program Files/boost/boost_1_69_0\boost/typeof/msvc/typeof_impl.hpp(163): warning: extra ";" ignored C:/Program Files/boost/boost_1_69_0\boost/numeric/ublas/vector.hpp(640): warning: invalid friend declaration C:/Program Files/boost/boost_1_69_0\boost/numeric/ublas/vector.hpp(1418): warning: invalid friend declaration C:/Program Files/boost/boost_1_69_0\boost/numeric/ublas/vector.hpp(2780): warning: invalid friend declaration C:/Program Files/boost/boost_1_69_0\boost/fusion/support/unused.hpp(25): warning: host annotation on a defaulted function("unused_type") is ignored C:/Program Files/boost/boost_1_69_0\boost/fusion/support/unused.hpp(25): warning: device annotation on a defaulted function("unused_type") is ignored C:/Program Files/boost/boost_1_69_0\boost/fusion/support/unused.hpp(31): warning: host annotation on a defaulted function("unused_type") is ignored C:/Program Files/boost/boost_1_69_0\boost/fusion/support/unused.hpp(31): warning: device annotation on a defaulted function("unused_type") is ignored 1 error detected in the compilation of "C:/Users/Amir/AppData/Local/Temp/tmpxft_000027e8_00000000-13_mexGpu.compute_70.cpp1.ii".
Error in mexcuda (line 157) [varargout{1:nargout}] = mex(mexArguments{:});
I have found that the problem is occur when odeint.hpp includes, if i remove it and it usage my function will compile successfully!
-
How can I check if a user has a C compiler configured or not?
I have a repository for an algorithm, one of the functions utilizes a mex file. There is an alternative function that does not use a mex file but its a more hard coded version of the function that utilizes the mex file. I want to check if the person has a C compiler installed and configured, and if they don't use the non mex function. What I'm looking for is create a conditional.
I've been playing around with the
mex.getCompilerConfigurations('C')
command and I am returned a compilerconfigurations object. I however have a compiler installed and configured (Mingw-w64). My concern is, I'm not sure what
mex.getCompilerConfigurations('C')
will return if no C compiler is found. Does it still return a compilerconfigurations object if there is no compiler or does it throw a warning, the documentation is lacking.
-
Probleme in installing and compiling the MEX interfaces
It is my first attempt at using
mex
files. I'm following and trying to reproduce the steps indicated here in order to use the ODE solvers.I have setup the programming language to C++.
>> mex -setup MEX configured to use 'MinGW64 Compiler (C)' for C language compilation. Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. You will be required to update your code to utilize the new API. You can find more information about this at: https://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html. To choose a different language, select one from the following: mex -setup C++ mex -setup FORTRAN Error using mex No supported compiler was found. For options, visit https://www.mathworks.com/support/compilers. MEX configured to use 'MinGW64 Compiler (C++)' for C++ language compilation. Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. You will be required to update your code to utilize the new API. You can find more information about this at: https://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
But when I run
mex -v
to check if everything is prepared and set for compilation.
I get the following error message:>> mex -v Verbose mode is on. Error using mex Not enough input arguments.
When I call
compile
as per the procedure in the aforementioned link, I get the error seen in the next screenshot :>> compile Verbose mode is on. ... Looking for compiler 'MinGW64 Compiler (C)' ... ... Looking for environment variable 'MW_MINGW64_LOC' ...Yes ('C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset'). ... Looking for file 'C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset\bin\gcc.exe' ...Yes. ... Looking for folder 'C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset' ...Yes. Found installed compiler 'MinGW64 Compiler (C)'. Set PATH = C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset\bin;C:\Program Files\MATLAB\R2018a\extern\include\win64;C:\Program Files\MATLAB\R2018a\extern\include;C:\Program Files\MATLAB\R2018a\simulink\include;C:\Program Files\MATLAB\R2018a\lib\win64;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\MATLAB\R2018a\runtime\win64;C:\Program Files\MATLAB\R2018a\bin;C:\Users\rayane benyoucef\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\ Set INCLUDE = C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset\include;;C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset\include;; Set LIB = C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset\lib;;C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset\lib;; Set MW_TARGET_ARCH = win64;win64; Set LIBPATH = C:\Program Files\MATLAB\R2018a\extern\lib\win64;C:\Program Files\MATLAB\R2018a\extern\lib\win64; Error using mex MEX cannot find library 'gfortran' specified with the -l option. MEX looks for a file with one of the names: libgfortran.lib gfortran.lib Please specify the path to this library with the -L option. Error in compile (line 75) mex('-c',args{:},'dop853Mex.c','dopri5Mex.c','options.c','tif.c');
Can anyone, give me a hand to find out what's the problem ?
Thanks.