Deriving cuda-class from c++ class, in large framework - c++

I have a quite large c++/mpi project in which I want to integrate some cuda-functionality.
I created a sample project (not runnable yet) to illustrate the problem. The comments in between the sources describe the problem.
I have a main.cc:
/*main.cc*/
#include <iostream>
#include "derivedclass.h"
#include "someotherclass.h"
using namespace std;
int main(){
int intstate = 4;
DerivedClass<int> myDerivedClass;
myDerivedClass.setState(intstate);
myDerivedClass.compute();
int result = myDerived.getResult();
SomeOtherClass mySomeOtherClass(result);
mySomeOtherClass.print();
}
which includes some c++ class(es):
/*someotherclass.h*/
#ifndef INTEGRATOR_H_GUARD
#define INTEGRATOR_H_GUARD
class SomeOtherClass{
private:
int someVariable;
public:
SomeotherClass(int someVariable);
void print();
};
#endif
/*someotherclass.cc*/
#include "someotherclass.h"
SomeOtherClass::SomeOtherClass(int someVariable){
this->someVariable = someVariable;
}
SomeOtherClass::print(){
cout << this->someVariable << endl;
}
These c++ parts a quite large and I don't won't to change them.
There is some baseclass:
/*baseclass.h*/
#ifndef BASECLASS_H_GUARD
#define BASECLASS_H_GUARD
class BaseClass{
protected:
int someVariable;
public:
BaseClass(int someVariable);
void compute();
int getResult();
};
#endif
/*baseclass.cc*/
BaseClass::BaseClass(int someVariable){
this->someVariable = someVariable;
}
void BaseClass::compute(){
/* do something*/
}
int BaseClass::getResult(){
return this->someVariable;
}
This base class provides a lot of functionality (too much but not my project so I can't change that).
Some methods are to be parallelized by me. So for me it seems to be the cleanest way to derive the BaseClass with a Class using Cuda:
/*derivedclass.h*/
#ifndef DERIVEDCLASS_H_GUARD
#define DERIVEDCLASS_H_GUARD
#include "baseclass.h"
class DerivedClass: public BaseClass{
public:
DerivedClass(int someVariable);
void compute();
};
#endif
/*derivedclass.cu*/
#include "derivedclass.h"
DerivedClass::DerivedClass(int someVariable):
BaseClass(someVariable);
{
}
void DerivedClass::compute(){
/* do some cuda stuff*/
}
So I have the following questions:
Is it possible to derive a cuda-class from a c++-class
When I try to compile the main.cc class with g++ and the .cu classes with nvcc I get an error when using cuda stuff in the .cu class for example cudamalloc:
./Folder/class.cu:line: Fehler: expected initializer before »cudaMalloc«
When I try to compile the main class with nvcc I get problems with MPI-functionality.
So is it possible to use a cuda-class from a main.cc compiled with g++/mpicc (which seems to be the right way round for me)?
I hope I described my problem understandable and appreciate any help.

From what I can tell from your code, your derived class is just a C++ class that launches CUDA kernels. So it's not really a "CUDA class". So the answer to your question 1. is "yes", you can do that. You just have to put any member functions that launch kernels in .cu files with the kernels they launch.
The error "expected initializer before 'cudaMalloc'" is a weird syntax error. It doesn't really seem to have anything to do with your derived class stuff. I suspect you just have a C++ syntax error.

Related

(solved) field has incomplete type of class

I´m building a program with several classes (data structures like stacks, lists,etc).
There is some class (Concesionario) that i need to use in another (ListaE). The class ListaE uses another class called NodoListaE, which uses two pointers, one to the value of the object (Concesionario) and another to the next position of the list (siguiente).
#ifndef NODOLISTAE_HPP
#define NODOLISTAE_HPP
#include "Concesionario.hpp"
class Concesionario;
class ListaE;
class NodoListaE
{
public:
NodoListaE(Concesionario* conc, NodoListaE* sig = NULL);
private:
Concesionario* conc;
NodoListaE* siguiente;
friend class ListaE;
};
typedef NodoListaE* pnodoListaE;
#endif // NODOLISTAE_HPP
#ifndef LISTAE_HPP
#define LISTAE_HPP
#include "NodoListaE.hpp"
#include "Automovil.hpp"
class Automovil;
class NodoListaE;
class ListaE
{
private:
NodoListaE* primero;
public:
ListaE();
void enlistarOrden(Automovil* automovil);
};
#endif // LISTAE_HPP
#ifndef CONCESIONARIO_HPP
#define CONCESIONARIO_HPP
#include <string>
#include "ListaE.hpp"
class ListaE;
class Concesionario
{
public:
Concesionario();
~Concesionario();
std::string mostrar();
void setZona(std::string letra);
void setNum();
int getNum();
private:
int nc=2;
int num_conc;
std::string zona;
int generadorNumsIntervalo(int min, int max);
ListaE automoviles;//ERROR HERE
};
#endif // CONCESIONARIO_HPP
All the cpp files are not implemented (empty constructor and destructor).
The compiler I´m currently using is MINGWx64.
I´ve tried using forward declarations and it worked for the rest of the classes but not for this one.
The program throws the following error in the **Concesionario ** hpp file: include\Concesionario.hpp|22|error: field 'automoviles' has incomplete type 'ListaE'|
Concesionario is implemented in other classes and the program runs perfectly.
Example of another class implementing Concesionario
#ifndef ARBOL_HPP
#define ARBOL_HPP
#include <iostream>
#include "NodoArbol.hpp"
#include "Concesionario.hpp"
using namespace std;
class Arbol {
public:
Arbol();
void Insertar(Concesionario* concesionario);
private:
pnodoArbol raiz;
pnodoArbol actual;
int contador;
int altura;
bool Vacio(pnodoArbol nodo);
};
#endif // ARBOL_HPP
I`ve also tried deleting this class and creating another one from 0 but the error remains.
Any solution to this problem? Thank you very much.

How do you inject dependencies in a class, that depends on a class, which depends on another class in C++?

I'm trying to implement dependency injection in a C++ project. However, due to the structure of the dependencies, I'm getting a segmentation fault which I can't solve.
As an example I constructed the following classes and interfaces. I have a class called MyClass which has a dependency on Dependency. Dependency has a dependency on OtherDependency. To allow for proper testing, I inherit the dependencies from an interface, i.e. IDependency and IOtherDependency. OtherDependency has a function some_function().
In main.cpp I create an instance of MyClass and then try to call some_function(). Unfortunately, this gives a segmentation fault:
Segmentation fault (core dumped)
MyClass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "IDependency.h"
class MyClass
{
public:
MyClass(IDependency *dependency);
~MyClass();
IDependency *_dependency = nullptr;
};
#endif
MyClass.cpp:
#include "MyClass.h"
#include <iostream>
MyClass::MyClass(IDependency *dependency) : _dependency(dependency) {}
MyClass::~MyClass() {}
Dependency.h:
#ifndef DEPENDENCY_H
#define DEPENDENCY_H
#include "IDependency.h"
#include "IOtherDependency.h"
class Dependency : public IDependency
{
public:
Dependency(IOtherDependency *other_dependency);
~Dependency();
IOtherDependency *_other_dependency = nullptr;
};
#endif
Dependency.cpp:
#include "Dependency.h"
#include <iostream>
Dependency::Dependency(IOtherDependency *other_dependency) : _other_dependency(other_dependency) {}
Dependency::~Dependency() {}
IDependency.h:
#ifndef IDEPENDENCY_H
#define IDEPENDENCY_H
#include "IOtherDependency.h"
class IDependency
{
public:
IOtherDependency *_other_dependency;
};
#endif
OtherDependency.h:
#ifndef OTHERDEPENDENCY_H
#define OTHERDEPENDENCY_H
#include "IOtherDependency.h"
class OtherDependency : public IOtherDependency
{
public:
OtherDependency();
~OtherDependency();
void some_function();
};
#endif
OtherDependency.cpp:
#include "OtherDependency.h"
#include <iostream>
OtherDependency::OtherDependency() {}
OtherDependency::~OtherDependency() {}
void OtherDependency::some_function()
{
std::cout << "I am OtherDependency." << std::endl;
}
IOtherDependency.h:
#ifndef IOTHERDEPENDENCY_H
#define IOTHERDEPENDENCY_H
class IOtherDependency
{
public:
virtual void some_function() = 0;
};
#endif
main.cpp:
int main()
{
OtherDependency *other_dependency = new OtherDependency;
Dependency *dependency = new Dependency(other_dependency);
MyClass my_class(dependency);
my_class._dependency->_other_dependency->some_function();
}
What am I doing wrong / do I need to change?
You have two variables called _other_dependency: one in IDependency, the other in Dependency. The Dependency constructor initialized the latter, while the one in the IDependency class retains its default nullptr value.
When you access my_class._dependency->_other_dependency, the other_dependency will be the one in IDependency, because _dependency points to the base class.
One way to fix this is to remove the other_dependency from Dependency, and pass the value from the Dependency constructor to IDependency to properly initialize its member.

Troubles with Circular Dependencies between 3 classes and with inheritance

I'm a first-year college student that doesn't know everything about CS yet, so please bear with my newness to it, and this is my first question on here.
For an assignment, we are making faux version of Pokemon Go to practice using polymorphism in c++, and I'm running into some compiler errors. Here are the three files with just a sample of the code in them:
#ifndef EVENT_H
#define EVENT_H
#include <string>
#include "Trainer.h"
class Event{
protected:
std::string title;
public:
Event();
~Event();
virtual void action(Trainer) = 0;
};
#endif
Trainer.h:
#ifndef TRAINER_H
#define TRAINER_H
#include "Pokemon.h"
class Trainer{
private:
Pokemon* pokemon;
int num_pokemon;
public:
Trainer();
~Trainer();
//include accessors and mutators for private variables
};
#endif
Pokemon.h:
#ifndef POKEMON_H
#define POKEMON_H
#include "Event.h"
#include <string>
class Pokemon : public Event{
protected:
std::string type;
std::string name;
public:
Pokemon();
~Pokemon();
virtual bool catch_pokemon() = 0;
};
#endif
The trainer.h file is a parent class for each pokemon type (eg Rock) which just defines a few virtual functions. The error I'm getting is when I'm compiling all of this and I get something that says:
Pokemon.h : 5:30: error: expected class-name befoer '{' token:
class Pokemon : Event {
Pokemon need to be a derived class to an event, so that an event pointer can point in another Location class can point to either a pokemon, pokestop, or cave for the assignment, and I have been looking online for hours and can't figure out what to do. I would appreciate the help! Let me know if you need more info or something because again, this is my first time posting a question.
You need some forward declarations.
In Event.h, you can put class Trainer; instead of #include "Trainer.h". In Trainer.h, you can put class Pokemon; instead of #include "Pokemon.h".
You will probably need to include the appropriate headers in the corresponding source files in order to actually use the other classes. But by avoiding the includes in the header files, you get out of the circular dependency trouble.
Pokemon.h must continue to #include "Event.h", since you're inheriting Event, which requires a complete definition.
Use forward declaration, to tell classes the type they need to use will be defined later. You can use forward declaration in situations where the size is know, pointers and references are always the same size regardless of the type they point to so use them.
#ifndef EVENT_H
#define EVENT_H
#include <string>
class Trainer;
class Event
{
protected:
std::string title;
public:
Event();
virtual ~Event();
virtual void action(Trainer* const trainer) = 0;
};
#endif
then
#ifndef TRAINER_H
#define TRAINER_H
class Pokemon;
class Trainer
{
private:
Pokemon* const pokemon;
int numPokemon;
public:
Trainer();
~Trainer();
};
#endif
then
#ifndef POKEMON_H
#define POKEMON_H
#include "Event.h"
#include <string>
class Pokemon : public Event
{
protected:
std::string type;
std::string name;
public:
Pokemon();
virtual ~Pokemon();
virtual bool catchPokemon() = 0;
};
#endif
when using polymorphism (virtual functions) you must always make the base class destructor virtual too. It is also nice to make the derived classes destructor virtual as well, but it is not required.

UML class diagram not generating for C++ code

I have read several questions about this problem at SO but the suggested solution is to do the UPDATE 4. I have already installed update-4 to my VS 2012 Ultimate. But still I am not able to get the UML diagram.
My Step:
Go to the architecture tab in VS 2012 Ultimate
select "New Diagram"
select "UML Class Diagram"
then choose the name etc.
the go to "Architecture Explorer"
Drop the classes for which you want to generate UML diagram.
Just to check if there is some other probelm, I wrote a simple program of two classes. In this program, the variable of one class is set by the variable of second class. But there is no UML diagram get generated for the following program.
ERRORS:
Reverse engineer C++ type is not supported. Some DGML node(s) will be skipped during the process. Please see warnings for details.
(0) type(s) fully reverse engineered.
Warnings:
<Warning Timestamp="2015-09-01T09:45:05" Importance="Medium" Text="Reverse engineer C++ type 'Class1' is skipped." />
<Warning Timestamp="2015-09-01T09:45:05" Importance="Medium" Text="Reverse engineer C++ type 'Class2' is skipped." />
My code:
Class1
#ifndef HEADER1_H
#define HEADER1_H
class Class1 {
private:
int value;
public:
void setValue(int value);
void showValue();
};
#endif
Class2:
#ifndef HEADER_H
#define HEADER_H
#include "Header1.h"
class Class2 {
public:
int secondClassValue;
void setValue(int val);
Class1 *class1;
};
#endif
main():
#include <iostream>
#include "Header1.h"
#include "Header.h"
using namespace std;
void Class1::setValue(int value) {
this->value = value;
}
void Class1::showValue() {
cout<<"The value of second class variable is: "<<this->value<<endl;
}
void Class2::setValue(int val) {
secondClassValue = val;
}
int main() {
Class1 *class1 = new Class1;
Class2 *class2 = new Class2;
class2->setValue(10);
class1->setValue(class2->secondClassValue);
//See the set value
class1->showValue();
delete class1;
delete class2;
return 0;
}

how to declare and use another class's object within a class in c++

I a beginner in programming.
I coded two classes(having constructors with requirement to pass arguments) and want to declare and use one class's object in another class.
I have tried to find the solution to my error on many website, but none of them worked. I also saw a solution to this problem using the 'new' syntax.
Please suggest some(any) way to sought out this problem.
A short program similar the one in which I am facing problems is as follows:
(I know this program is stupid but, this is not actual program I am facing problem in. Instead this is a narrowed down version of the part of the program in which I am facing error)
The error is in Class2.h and main.cpp
main.cpp
#include <iostream>
#include "Class2.h"
using namespace std;
int main()
{
Class2 Class2_Obj;
Class2_Obj.Class2_Function(); // error: undefined reference to `Class2::Class2_Function
return 0;
}
Class1.h
#ifndef CLASS1_H_INCLUDED
#define CLASS1_H_INCLUDED
class Class1
{
private:
const int c1_Variable;
public:
Class1(int);
// Displays the value of c1_Variable on output screan
void Class1_Function();
};
#endif // CLASS1_H_INCLUDED
Class1.cpp
#include <iostream>
#include "Class1.h"
Class1::Class1(int receivedInt) : c1_Variable(receivedInt) {}
void Class1::Class1_Function()
{
cout << c1_Variable;
}
Class2.h
#ifndef CLASS2_H_INCLUDED
#define CLASS2_H_INCLUDED
#include"Class1.h"
class Class2
{
private:
Class1 Class1_Obj(4); // 4 is just a random number.
//error: expected identifier before numeric constant
//error: expected ',' or '...' before numeric constant
public:
// Calls Class1_Function()
void Class2_Function();
};
#endif // CLASS2_H_INCLUDED
Class2.cpp
#include <iostream>
#include "Class1.h"
#include "Class2.h"
void Class::Class2_Function()
{
Class1_Obj.Class1_Function();
}
Here are the links to snapshots of the errors:
Screenshot of Error in Class2.h - http://i.stack.imgur.com/WpK9k.jpg
Screenshot of Error in main.cpp - http://i.stack.imgur.com/yDBD7.jpg
Please help me out! Thanks in advance for any responses :)
The issue is that this in-place initialization of non-static data members syntax is invalid:
class Class2
{
private:
Class1 Class1_Obj(4);
....
};
You can use {} instead,
class Class2
{
private:
Class1 Class1_Obj{4};
....
};
or this form
class Class2
{
private:
Class1 Class1_Obj = Class1(4);
....
};
C++ is a Object Oriented Language. It has classes to structure its data.
To put one class into another, you make an object of one class a member of another class.
Syntactically, it works like
class A {
int x;
public:
A (int x1) : x(x1) {}
};
class B {
A a; // this is how you do it ..
public:
B() : A(4) {}
};
B b; // b is an object which has a member b.a
As you can see, b is an object of class B. It has a member a of class A.