CUSP library called from Fortran not working - nvcc

I want to repetitively solve the CG/BicGSTAB using CUSP solver, called from Fortran. To avoid transfers I am passing the Fortran data directly to CUSP. The code compiles but breaks at the run time flagging:
terminate called after throwing an instance of 'thrust::system::system_error'
what(): invalid argument
terminate called recursively
Aborted (core dumped)
Let alone the core of the code, even the print stream is not happening. The code of course is in the preliminary stage, but I wonder what is wrong with it.
extern "C" void bicgstab_(int *device_I, int *device_J, float *device_V, float *device_x, float *device_b, int *n, int *nnz){
int N = *n;
int NNZ = *nnz;
std::cout << N << " " << NNZ << " " << *device_I << std::endl;
for(int i=0; i<N;i++)std::cout << device_I[i] << " "; std::cout << std::endl;
for(int i=0; i<NNZ;i++)std::cout << device_J[i] << " "; std::cout << std::endl;
for(int i=0; i<NNZ;i++)std::cout << device_V[i] << " "; std::cout << std::endl;
for(int i=0; i<N;i++)std::cout << device_x[i] << " "; std::cout << std::endl;
for(int i=0; i<N;i++)std::cout << device_b[i] << " "; std::cout << std::endl;
// *NOTE* raw pointers must be wrapped with thrust::device_ptr!
thrust::device_ptr<int> wrapped_device_I(device_I);
thrust::device_ptr<int> wrapped_device_J(device_J);
thrust::device_ptr<float> wrapped_device_V(device_V);
thrust::device_ptr<float> wrapped_device_x(device_x);
thrust::device_ptr<float> wrapped_device_b(device_b);
// use array1d_view to wrap the individual arrays
typedef typename cusp::array1d_view< thrust::device_ptr<int> > DeviceIndexArrayView;
typedef typename cusp::array1d_view< thrust::device_ptr<float> > DeviceValueArrayView;
std::cout << wrapped_device_I[3];
/*
DeviceIndexArrayView row_indices (wrapped_device_I, wrapped_device_I + (N+1));
DeviceIndexArrayView column_indices(wrapped_device_J, wrapped_device_J + NNZ);
DeviceValueArrayView values (wrapped_device_V, wrapped_device_V + NNZ);
DeviceValueArrayView x (wrapped_device_x, wrapped_device_x + N);
DeviceValueArrayView b (wrapped_device_b, wrapped_device_b + N);
// std::cout << device_x[0] ;
// for(int i=0;i<NNZ;i++)std::cout << column_indices[i] << std::endl;
// combine the three array1d_views into a csr_matrix_view
typedef cusp::csr_matrix_view<DeviceIndexArrayView,
DeviceIndexArrayView,
DeviceValueArrayView> DeviceView;
// construct a csr_matrix_view from the array1d_views
DeviceView A(N, N, NNZ, row_indices, column_indices, values);
// set stopping criteria: // iteration_limit = 100 // relative_tolerance = 1e-5
cusp::verbose_monitor<float> monitor(b, 100, 1e-5);
// solve the linear system A * x = b with the Conjugate Gradient method
// cusp::krylov::bicgstab(A, x, b);*/
}
If this is not feasible, I can move over to another approach,but as I am not sure about the correctness, I am unable to decide. Any help is appreciated.

Related

c++ Getting the Summation of numbers using an array and pointers

So I was doing a program where i need to find the total area of all floors, where the floors are determined by the user. I think I'm correct on the part to use the pointers, so to check if the code was correct I tried it doing it with basic addition. but even with basic addition it seems to have already some problems. I tried looking for similar questions in here and I can't find anything that might help me, so I hope you guys can help me. Thank you in advance.
float *Length=NULL, *Width=NULL, *Area=NULL, TotalArea, templ, tempw;
int floors, count;
cout << "Input the number of floors to proceed\n";
cout << ":";
cin >> floors;
Length = new float[floors];
Width = new float[floors];
Area = new float[floors];
for (int loop = 0; loop < floors; loop++)
{
cout << "\n\nFloor " << loop + 1 << endl;
cout << "Input the Length: \n";
cin >> templ;
cout << "Input the Width: \n";
cin >> tempw;
*(Length + loop) = templ;
*(Width + loop) = tempw;
*(Area + loop) = (*Length + loop) + (*Width + loop);
count = loop;
for (int count = 0; count < floors; count++)
{
TotalArea = TotalArea + *Area+count;
}
}
cout << TotalArea << endl;
I tried inputting the following:
floor:2
floor 1
length: 1
width: 1
floor 2
length: 1
width: 1
The answer should be 4, but the output ends up with 10.
You don't need arrays and pointers. Since you are looping through the floors and summing the areas of each floor, you can "forget" about previously encountered lengths, widths and areas, and only remember the total area encountered so far.
float length;
float width;
float area;
float totalArea;
int floors;
std::cout << "Input the number of floors to proceed\n";
std::cout << ":";
std::cin >> floors;
totalArea = 0;
for (int loop = 0; loop < floors; loop++)
{
std::cout << "\n\nFloor " << loop + 1 << std::endl;
std::cout << "Input the Length: " << std::endl;
std::cin >> length;
std::cout << "Input the Width: " << std::endl;
std::cin >> width;
area = length * width;
totalArea += area;
}
std::cout << totalArea << std::endl;
Notice how I added these annoying std:: everywhere? If you wonder why, see this other question: Why is “using namespace std;” considered bad practice?
I tried making some changes based on you guys comments and here is what I did.
There are some warnings I don't really know why but It seems to be working
float* Length = NULL, * Width = NULL, TotalArea, tempw, templ, * Area = NULL;
int floors, loop = 0;
cout << "Input the number of floors to proceed\n";
cout << ":";
cin >> floors;
for (int loop = 0; loop < floors; loop++)
{
Length = new float[floors];
Width = new float[floors];
Area = new float[floors];
cout << "\n\nFloor " << loop + 1 << endl;
cout << "Input the Length: \n";
cin >> templ;
cout << "Input the Width: \n";
cin >> tempw;
*(Length +loop) = l;
*(Width+loop) = w;
Area[floors] = (*(Length+loop)) * (*(Width+loop));
TotalArea += a[floors];
}
cout << TotalArea << endl;
This is what I came u with, it seems to be working, the output is also correct now.

Pointer to char outputs differently compared to other primitives?

I have this code that produces an unexpected result for a char pointer:
#include <iostream>
#include <stdlib.h>
#include <string>
void main(int argv, char* argc[]) {
char* test1 = new char[7];
test1[0] = 'H';
test1[1] = 'a';
test1[2] = 'l';
test1[3] = 'e';
test1[4] = 't';
test1[5] = 'y';
test1[6] = 'a';
char* t2 = &test1[3];
std::cout << *t2 << std::endl;
std::cout << t2 << std::endl;
std::cout << std::endl;
int v[] = { 1,2,3,4 };
int* v2 = &v[0];
std::cout << *v2 << std::endl;
std::cout << v2 << std::endl;
std::cout << std::endl;
float f[] = { 0.2, 0.4, 0.6 };
float* f2 = &f[1];
std::cout << *f2 << std::endl;
std::cout << f2 << std::endl;
std::cout << std::endl;
std::string str = "A_string";
std::string *str2 = &str;
std::cout << *str2 << std::endl;
std::cout << str2 << std::endl;
std::cout << std::endl;
std::cin.ignore();
}
The output for this is variable but usually something like this:
What's going on? For the int, float, and string, printing the address prints the pointer address and printing the pointer prints the correct value. However, for the char array, while printing the pointer also prints the correct value, printing the address prints the last 4 elements of the array and a bunch of crap that varies per run.
Is there something about char that causes this or do I just have a funky setup? I've tried initializing the char array with brackets but it's the same result.
Never mind. I guess the solution is this:
std::cout << *t2 << std::endl;
std::cout << (void*)t2 << std::endl;
std::cout << std::endl;
Also found:
Why is address of char data not displayed?

Boost C++ Regex Example Compile Error

I'm having an extremely frustrating time trying to get the Boost regex library to behave itself in XCode 8.
I've finally managed to sort the includes out, now I'm hitting compiler errors when attempting to run the following regex example from Boost's library documentation here.
The code is as follows:
void print_captures(const std::string& regx, const std::string& text)
{
boost::regex e(regx);
boost::smatch what;
std::cout << "Expression: \"" << regx << "\"\n";
std::cout << "Text: \"" << text << "\"\n";
if(boost::regex_match(text, what, e, boost::match_extra))
{
unsigned i, j;
std::cout << "** Match found **\n Sub-Expressions:\n";
for(i = 0; i < what.size(); ++i)
std::cout << " $" << i << " = \"" << what[i] << "\"\n";
std::cout << " Captures:\n";
for(i = 0; i < what.size(); ++i)
// compiler error in line above
{
std::cout << " $" << i << " = {";
for(j = 0; j < what.captures(i).size(); ++j)
// compiler erro in line above
{
if(j)
std::cout << ", ";
else
std::cout << " ";
std::cout << "\"" << what.captures(i)[j] << "\"";
}
std::cout << " }\n";
}
}
else
{
std::cout << "** No Match found **\n";
}
}
int main(int , char* [])
{
print_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee");
print_captures("(.*)bar|(.*)bah", "abcbar");
print_captures("(.*)bar|(.*)bah", "abcbah");
print_captures("^(?:(\\w+)|(?>\\W+))*$",
"now is the time for all good men to come to the aid of the party");
return 0;
}
I'm getting two errors at the points indicated in the block above that both read:
No member named 'captures' in 'boost::match_results<std::__1::__wrap_iter<const char *>, std::__1::allocator<boost::sub_match<std::__1::__wrap_iter<const char *> > > >'
Apologies in advance if this question has been answered elsewhere.
Any idea how I can fix this?

How do I input more than one dynamic array integer?

How would I modify this code so that I can enter in more than just one coefficient?
I am suppose to be able to enter in something like "3 2 1" and the spaces shouldn't affect my user input but I'm not sure how to do this.
This is the code I have so far:
#include <iostream>
using namespace std;
int *foil(int A[], int B[], int co, int coo)
{
int *product = new int[co+coo-1];
for (int i = 0; i<co+coo-1; i++)
product[i] = 0;
for (int i=0; i<coo; i++)
{
for (int j=0; j<co; j++)
product[i+j] += A[i]*B[j];
}
return product;
}
void printPoly(int poly[], int co)
{
for (int i=0; i<co; i++)
{
cout << poly[i];
if (i != 0)
cout << "x^" << i ;
if (i != co-1)
cout << " + ";
}
}
int main()
{
int co, coo;
int *A;
A=new int[co];
int *B;
B=new int[coo];
cout << "How many coefficients are in the first poly?: ";
cin >> co;
cout << "What are the coefficients? (Lowest power first): ";
cin >> *A;
cout << "How many coefficients are in the second poly?: ";
cin >>coo;
cout << "What are the coefficients? (Lowest power first): ";
cin >>*B;
printPoly(A, coo);
cout << "\n";
cout << "times" << endl;
printPoly(B, co);
cout << "\n";
cout << "-----" << endl;
int *product = foil(A, B, co, coo);
printPoly(product, co+coo-1);
return 0;
}
It outputs this:
How many coefficients are in the first poly?: 3
What are the coefficients? (Lowest power first): 3 2 1
How many coefficients are in the second poly?: What are the coefficients? (Lowest power first): 3 + 0x^1
times
1 + 0x1 + 0x^2
-----
3 + 0x^1 + 0x^2 + 0x^3
I want it to output it like this:
How many coefficients are in the first poly?: 3
What are the coefficients? (Lowest power first): 3 2 1
How many coefficients are in the second poly? 3
What are the coefficients? (Lowest power first): 1 2 1
3 + 2x^1 + 1x^2 
times 
1 + 2x^1 + 1x^2 
-----­­­­­ 
3 + 8x^1 + 8x^2 + 4x^3 + 1x^4
you need to take the inputs in a loop.
here is how you can do it.
cout << "How many coefficients are in the first poly?: ";
cin >> co;
cout << "What are the coefficients? (Lowest power first): ";
for(int i=0;i<co;i++)
cin >> *(A+i);
cout << "How many coefficients are in the second poly?: ";
cin >>coo;
cout << "What are the coefficients? (Lowest power first): ";
for(int i=0;i<coo;i++)
cin >>*(B+i);
printPoly(A, coo);
cout << "\n";
cout << "times" << endl;
printPoly(B, co);
cout << "\n";
cout << "-----" << endl;
int *product = foil(A, B, co, coo);
printPoly(product, co+coo-1);
getch();
return 0;
Here is the output image

Passing by reference character arrays

I'm attempting to pass a character array located in _name into a char reference called name.
However, when passing the array pointer into the reference, it would ONLY display the first character rather than the whole string.
My question is how would you create a Character reference array to copy the original pointer into it then displaying it? As show in item.cpp we copy _name pointer into reference of name then return name, it however only displays the first character of the string.
I will only show the relevant pieces of my code.
Item.cpp:
void Item::name(const char * name){
strncpy(_name, name , 20);
}
const char& Item::name() const{
char& name = *_name;
return name;
}
ItemTester.cpp:
Main():
int main(){
double res, val = 0.0;
fstream F;
SItem Empty;
SItem A("456", "AItem", 200);
SItem B("567", "BItem", 300, false);
//cout << A.name() << endl;
B.quantity(50);
//cout << Empty << endl;
cout << A << endl << B << endl << endl;
cout << "Enter Item info for A: (Enter 123 for sku)" << endl;
cin >> A;
cout << "Copying A in C ----" << endl;
SItem C = A;
cout << C << endl << endl;
cout << "Saving A---------" << endl;
A.save(F);
cout << "Loading B----------" << endl;
B.load(F);
cout << "A: ----------" << endl;
cout << A << endl << endl;
cout << "B: ----------" << endl;
cout << B << endl << endl;
cout << "C=B; op=----------" << endl;
C = B;
cout << C << endl << endl;;
cout << "Operator ==----------" << endl;
cout << "op== is " << ((A == "123") && !(A == "234") ? "OK" : "NOT OK") << endl << endl;
cout << "op+=: A += 20----------" << endl;
A += 20;
cout << A << endl << endl;
cout << "op-=: A -= 10----------" << endl;
A -= 10;
cout << A << endl << endl;
cout << "op+=double: ----------" << endl;
res = val += A;
cout << res << "=" << val << endl << endl;
return 0;
}
ostream write
virtual std::ostream& write(std::ostream& os, bool linear)const{
return os << sku() << ": " << name() << endl
<< "Qty: " << quantity() << endl
<< "Cost (price * tax): " << fixed << setprecision(2) << cost();
}
Let me know if i missed any important details and il edit my post with it.
char& is reference to char, thus just a single character. Reference to array of characters would be char*&.
Example:
class Test
{
private:
static const size_t maxlen = 100;
char* _name;
public:
Test() : _name(new char[maxlen+1]) {}
~Test() {delete _name;}
void name(const char* s)
{
if(strlen(s) >= maxlen)
throw "too long";
else
{
memcpy(_name, s, strlen(s) * sizeof(char));
_name[strlen(s)] = '\0';
}
}
char*& name()
{
return _name;
}
};
int main()
{
Test obj;
obj.name("testname");
cout<<"Name = "<<obj.name()<<endl;
obj.name()[0] = '*';
cout<<"After change: Name = "<<obj.name()<<endl;
return 0;
}
EDIT:
I would change "getter" to something like:
char*& Item::name() {
return _name;
}
Actually if you do want the method to be "const", in the sense that user of the class should not change the elements of the array, or the actual address of the array, then you need not return a char*&, you can simply return const char*
const char* Item::name() const {
return _name;
}
As far as I see, the purpose of a char*& type is that the client would be able to change the actual address of an address.
As CForPhone pointed out, char& is not really what you want, you probably meant char*. But even then, using char* to represent strings is for C. In C++, you should use std::string:
const string Item::name() const{
string name(_name);
return name;
}

Resources