function rand() codeblocks doesnt compile - random

im trying to compile this code in codeblocks but doesnt work, i dont know what it is produced error
#include<iostream>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
const int NUMLANZ=1000;
int caras=0,cruces=0,i;
double volado,porcaras,porcruces;
srand(time(NULL));
for(i=1;1<=NUMLANZ; i++)
{
volado= ((double) rand() / (RAND_MAX));
if(volado>0.5)
caras=caras+1;
else
cruces=cruces+1;
}
porcaras=(caras/double (NUMLANZ))*100;
porcruces=(cruces/ double(NUMLANZ))*100;
cout<<"\nCaras salio "<<porcaras<<" por ciento de las veces";
cout<<"\nCruces salio "<<porcruces<<" por ciento de las veces";
return 0;
}
im new in c++ code, i dont know if the compile produced error becaue this code was copied from c++ book study so its strange that this code doesnt work

The loop will never terminate because the literal constant 1 is always less that NUMLANZ.
Compare:
for( i = 1; 1 <= NUMLANZ; i++ )
^
with
for( i = 1; i <= NUMLANZ; i++ )
^
Note also that it is more conventional for loop counters to start from zero:
for( i = 0; i < NUMLANZ; i++ )
In this case it makes no difference other then one less character, but if you are using i to index an array for example, it is critical.

Related

how to debug a CUDA google colab notebook?

I am trying to run a c program using cuda the code does some math operations on an array of consecutive numbers (where every thread add elements of a row and check the last array element and return a value of the sum or zero if the conditions are met).
I don't have NVIDIA GPU so I wrote my code on google colab notebook.
The problem that I have encountered was not being able to debug the program. It outputs nothing at all no error messages and no output.
There's something wrong with the code but I cannot know where after reviewing it a few times.
Here's the code:
#include <iostream>
__global__ void matrixadd(int *l,int *result,int digits ,int possible_ids )
{
int sum=0;
int zeroflag=1;
int identicalflag=1;
int id= blockIdx .x * blockDim .x + threadIdx .x;
if(id<possible_ids)
{
if (l[(digits*id)+digits-1]==0) zeroflag=0;/*checking if the first number is zero*/
for(int i=0; i< digits-1;i++)/*edited:for(int i=0; i< digits;i++) */
{
if(l[(digits*id)+i]-l[(digits*id)+i+1]==0)
identicalflag+=1; /* checking if 2 consequitive numbers are identical*/
sum = sum + l[(digits*id)+i]; /* finding the sum*/
}
if (identicalflag!=1)identicalflag=0;
result[id]=sum*zeroflag*identicalflag;
}
}
int main()
{
int digits=6;
int possible_ids=pow(10,digits);
/*populate the array */
int* a ;
a= (int *)malloc((possible_ids * digits) * sizeof(int));
int the_id,temp=possible_ids;
for (int i = 0; i < possible_ids; i++)
{
temp--;
the_id=temp;
for (int j = 0; j < digits; j++)
{
a[i * digits + j] = the_id % 10;
if(the_id !=0) the_id /= 10;
}
}
/*the numbers will appear in reversed order */
/*allocate memory on host and device then invoke the kernel function*/
int *d_a,*d_c,*c;
int size=possible_ids * digits;
c= (int *)malloc(possible_ids * sizeof(int));/*results matrix*/
cudaMalloc((void **)&d_a,size*sizeof(int));
cudaMemcpy(d_a,a,size*sizeof(int),cudaMemcpyHostToDevice);
cudaMalloc((void **)&d_c,possible_ids*sizeof(int));
/*EDITED: cudaMalloc((void **)&d_c,digits*sizeof(int));*/
matrixadd<<<ceil(possible_ids/1024.0),1024>>>(d_a,d_c,digits,possible_ids);
cudaMemcpy(c,d_c,possible_ids*sizeof(int),cudaMemcpyDeviceToHost);
int acc=0;
for (int k=0;k<possible_ids;k++)
{
if (c[k]==7||c[k]==17||c[k]==11||c[k]==15)continue;
acc += c[k];
}
printf("The number of possible ids %d",acc);
}
You are doing invalid indexing into the l array in this line of code: if(l[(digits*id)+i]-l[(digits*id)+i+1]==0)
From comment by Robert Covella
If you are using python code, you can use 'pdb' built-in breakpoint function. put the following line of command at the top of your script.
import pdb
then before the line, you want to debug put the following command
pdb.set_trace()
you will get '(Pdb), then empty box' to insert the command. If you want to continue to the next line put 'n' or you can use 's' to see the detailed work of your current line command.
Suppose you are interested in debugging python code. Enjoy it!

Unsigned char out of range

I am trying to figure out, how to use an unsigned char type of a variable inside a for loop, while not "breaking" out of range for unsigned char, which can vary form 0 to 255.
main(void) {
TRISC = 0;
LATC = 0;
unsigned char j;
for (j = 0; j <= 255 ; j++){
LATC = j;
__delay_ms(1000);
}
return;
}
This is code in C, where PIC is programmed. "TRISC = 0" means setting port C as an output and "LATC" is referring to port C itself. Basically I want to assign values from including 0 to 255 to this port. But if I try to compile this, the compiler (xc8) returns following two warnings:
I cannot quite understand what these two are saying, but I assume it has to do something with variable j exceeding the limit value of unsigned char, that is 255 (in last iteration j = 256, which is not allowed/defined).
However, this code gets compiled and works as meant. But I still want to write and understand a code that assigns port C the value of 255 without entering "prohibited" range of values.
*P.S. I would use any other variable type than unsigned char or char, however to ports in PICs only these two types can be applied directly (without conversion).
j <= 255 is always true if j is only 8 Bit wide.
This version should work:
main(void) {
TRISC = 0;
LATC = 0;
int j;
for (j = 0; j <= 255 ; j++){
LATC = (unsigned char)j;
__delay_ms(1000);
}
return;
}
First, in microcontroller firmware, you should not return from main(). Your main() should include some kind of endless loop.
j <= 255 is always true for a uint8_t variable. Because j can't be 256. Adding 1 to j when it's 255, makes it 0, not 256.
As others have suggested, using an 16-bit integer, signed or unsigned, is the easiest and the cleanest way. However, in performance sensitive loops you may prefer to stick with 8 bit loop counters as these are the fastest ones for a 8-bit PIC microcontroller.
This particular one-time loop can be written as:
uint8_t j = 0;
do {
LATC = j++;
__delay_ms(1000);
} while (j != 0);

why am i getting access violation error c++?

i am getting 0xc0000005 error(access violation error), where am i wrong in this code?
i couldnt debug this error. please help me.
question is this
Formally, given a wall of infinite height, initially unpainted. There occur N operations, and in ith operation, the wall is painted upto height Hi with color Ci. Suppose in jth operation (j>i) wall is painted upto height Hj with color Cj such that Hj >= Hi, the Cith color on the wall is hidden. At the end of N operations, you have to find the number of distinct colors(>=1) visible on the wall.
#include<iostream>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
for(int tt= 0;tt<t;tt++)
{
int h,c;
int temp = 0;
cin>>h>>c;
int A[h], B[c];
vector<int> fc;
for(int i = 0;i<h;i++)
{
cin>>A[i];
}
for(int j =0;j<h;j++)
{
cin>>B[j];
}
if(is_sorted(A,A+h))
{
return 1;
}
if(count(A,A+h,B[0]) == h)
{
return 1;
}
for(int i = 0;i<h;i++)
{
if(A[i]>=temp)
{
temp = A[i];
}
else
{
if(temp == fc[fc.size()-1])
{
fc[fc.size()-1] = B[i];
}
else
{
fc.push_back(B[i]);
}
}
}
}
}
There are several issues.
When reading values into B, your loop check is j<h. How many elements are in B?
You later look at fc[fc.size()-1]. This is Undefined Behavior if fc is empty, and is the likely source of your problem.
Other issues:
Don't use #include <bits/stdc++.h>
Avoid using namespace std;
Variable declarations like int A[h], where h is a variable, are not standard C++. Some compilers support them as an extension.

Incorrect simultaneous operation of OpenMP and MPIR in VS 2015

Guys. I'm trying to speed up the loop using OpenMP.
If I speed up a loop that uses an integer variable, then everything works correctly:
void main()
{
//mpz_class i("0");
//mpz_class k("1");
//mpz_class l("1211728594799");
int k = 9;
int i = 0;
int l = 1998899087;
#pragma omp parallel for
for (i=k; i <= l; i++) {
if (i == 1998899085)
printf("kkk");
}
system("pause");
}
If I start using the MPIR variable in a loop, then I get errors when building the program in Visual Studio 2015. Here are the numbers of these errors: C3015, C3017,C3019. Here is the code that causes these errors:
void main()
{
mpz_class i("0");
mpz_class k("1");
mpz_class l("1211728594799");
//int k = 9;
//int i = 0;
//int l = 1998899087;
#pragma omp parallel for
for (i=k; i <= l; i++) {
if (i == 1998899085)
printf("kkk");
}
system("pause");
}
MPIR itself works correctly if I disable pragma omp parallel for then the code is going to be fine, but it works much slower than using int variable of the same range of numbers.
What should I do to make Open MP work correctly with MPIR and I could speed up my program by running it in parallel?

Eigen JacobiSVD cuda compile error

I've got an error, regarding calling JacobiSVD in my cuda function.
This is the part of the code that causing the error.
Eigen::JacobiSVD<Eigen::Matrix3d> svd( cov_e, Eigen::ComputeThinU | Eigen::ComputeThinV);
And this is the error message.
CUDA_voxel_building.cu(43): error: calling a __host__
function("Eigen::JacobiSVD , (int)2> ::JacobiSVD") from a __global__
function("kernel") is not allowed
I've used the following command to compile it.
nvcc -std=c++11 -D_MWAITXINTRIN_H_INCLUDED -D__STRICT_ANSI__ -ptx CUDA_voxel_building.cu
I'm using code 8.0 with eigen3 on ubuntu 16.04.
It seems like other functions such as eigen value decomposition also gives the same error.
Anyone knows a solution? I'm enclosing my code below.
//nvcc -ptx CUDA_voxel_building.cu
#include </usr/include/eigen3/Eigen/Core>
#include </usr/include/eigen3/Eigen/SVD>
/*
#include </usr/include/eigen3/Eigen/Sparse>
#include </usr/include/eigen3/Eigen/Dense>
#include </usr/include/eigen3/Eigen/Eigenvalues>
*/
__global__ void kernel(double *p, double *breaks,double *ind, double *mu, double *cov, double *e,double *v, int *n, char *isgood, int minpts, int maxgpu){
bool debuginfo = false;
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if(debuginfo)printf("Thread %d got pointer\n",idx);
if( idx < maxgpu){
int s_ind = breaks[idx];
int e_ind = breaks[idx+1];
int diff = e_ind-s_ind;
if(diff >minpts){
int cnt = 0;
Eigen::MatrixXd local_p(3,diff) ;
for(int k = s_ind;k<e_ind;k++){
int temp_ind=ind[k];
//Eigen::Matrix<double, 3, diff> local_p;
local_p(1,cnt) = p[temp_ind*3];
local_p(2,cnt) = p[temp_ind*3+1];
local_p(3,cnt) = p[temp_ind*3+2];
cnt++;
}
Eigen::Matrix3d centered = local_p.rowwise() - local_p.colwise().mean();
Eigen::Matrix3d cov_e = (centered.adjoint() * centered) / double(local_p.rows() - 1);
Eigen::JacobiSVD<Eigen::Matrix3d> svd( cov_e, Eigen::ComputeThinU | Eigen::ComputeThinV);
/* Eigen::Matrix3d Cp = svd.matrixU() * svd.singularValues().asDiagonal() * svd.matrixV().transpose();
mu[idx]=p[ind[s_ind]*3];
mu[idx+1]=p[ind[s_ind+1]*3];
mu[idx+2]=p[ind[s_ind+2]*3];
e[idx]=svd.singularValues()(0);
e[idx+1]=svd.singularValues()(1);
e[idx+2]=svd.singularValues()(2);
n[idx] = diff;
isgood[idx] = 1;
for(int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
v[x+ 3*y +idx*9]=svd.matrixV()(x, y);
cov[x+ 3*y +idx*9]=cov_e(x, y);
//if(debuginfo)printf("%f ",R[x+ 3*y +i*9]);
if(debuginfo)printf("%f ",Rm(x, y));
}
}
*/
} else {
mu[idx]=0;
mu[idx+1]=0;
mu[idx+2]=0;
e[idx]=0;
e[idx+1]=0;
e[idx+2]=0;
n[idx] = 0;
isgood[idx] = 0;
for(int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
v[x+ 3*y +idx*9]=0;
cov[x+ 3*y +idx*9]=0;
}
}
}
}
}
First of all, Ubuntu 16.04 provides Eigen 3.3-beta1, which is not really recommended to be used. I would suggest upgrading to a more recent version. Furthermore, to include Eigen, write (e.g.):
#include <Eigen/Eigenvalues>
and compile with -I /usr/include/eigen3 (if you use the version provided by the OS), or better -I /path/to/local/eigen-version.
Then, as talonmies noted, you can't call host-functions from kernels, (I'm not sure at the moment, why JacobiSVD is not marked as device function), but in your case it would make much more sense to use Eigen::SelfAdjointEigenSolver, anyway. Since the matrix you are decomposing is fixed-size 3x3 you should actually use the optimized computeDirect method:
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eig; // default constructor
eig.computeDirect(cov_e); // works for 2x2 and 3x3 matrices, does not require loops
It seems the computeDirect even works on the beta version provided by Ubuntu (I'd still recommend to update).
Some unrelated notes:
The following is wrong, since you should start with index 0:
local_p(1,cnt) = p[temp_ind*3];
local_p(2,cnt) = p[temp_ind*3+1];
local_p(3,cnt) = p[temp_ind*3+2];
Also, you can write this in one line:
local_p.col(cnt) = Eigen::Vector3d::Map(p+temp_ind*3);
This line will not fit (unless diff==3):
Eigen::Matrix3d centered = local_p.rowwise() - local_p.colwise().mean();
What you probably mean is (local_p is actually 3xn not nx3)
Eigen::Matrix<double, 3, Eigen::Dynamic> centered = local_p.colwise() - local_p.rowwise().mean();
And when computing cov_e you need to .adjoint() the second factor, not the first.
You can avoid both 'big' matrices local_p and centered, by directly accumulating Eigen::Matrix3d sum2 and Eigen::Vector3d sum with sum2 += v*v.adjoint() and sum +=v and computing
Eigen::Vector3d mu = sum / diff;
Eigen::Matrix3d cov_e = (sum2 - mu*mu.adjoint()*diff)/(diff-1);

Resources