Too many initializers while printing char 2D array - dev-c++

#include<iostream>
using namespace std;
// bool turnright(char **arr,int &x,int &y,bool &quit)
// {}
// bool turnright(char **arr,int &x,int &y,bool &quit)
// {}
// bool moveforward(char **arr,int &x,int &y,bool &quit)
// {}
// bool movebackward(char **arr,int &x,int &y,bool &quit)
// {
// }
void print(char arr[][12])
{
for (int i=0;i<12;i++)
{
for (int j=0;j<12;j++)
{
cout<<arr[i][j];
}
cout<<endl;
}
}
int main()
{
char arr[12][12]={
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','.','.','.','#','.','.','.','.','.','.','#'},
{'#','.','#','.','#','.','#','#','#','#','.','#'},
{'#','#','#','.','#','.','.','.','.','#','.','#'},
{'#','.','.','.','.','#','#','#','.','#','.','.'},
{'#','.','.','.','.','#','#','#','.','#','.','.'},
{'#','.','.','#','.','#','.','#','.','#','.','#'},
{'#','#','.','#','.','#','.','#','.','#','.','#'},
{'#','.','.','.','.','.','.','.','.','#','.','#'},
{'#','#','#','#','#','#','.','#','#','#','.','#'},
{'.','.','.','.','.','.','.','.','#','.','.','.','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#'}
};
// bool quit=false;
// int Ix;
// int IY;
// while(!quit)
// {
// moveforward();
// turnfight();
// if (!moveforward())
// {
// turnleft();
// if (!turnleft)
// {
// moveback();
// turnleft();
// if(!turnleft())
// {
// turnright();
// }
// }
// }
// }
print(arr);
}
I am trying to write code for traversing a maze while printing maze I am getting an error too many initiallizers although i have given number of rows and columns properly could any one please tell me where i am wrong ...

I reformatted your array initialization and see that row 11 has 13 elements which won't fit in an [12][12] array:
char arr[12][12]={
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','.','.','.','#','.','.','.','.','.','.','#'},
{'#','.','#','.','#','.','#','#','#','#','.','#'},
{'#','#','#','.','#','.','.','.','.','#','.','#'},
{'#','.','.','.','.','#','#','#','.','#','.','.'},
{'#','.','.','.','.','#','#','#','.','#','.','.'},
{'#','.','.','#','.','#','.','#','.','#','.','#'},
{'#','#','.','#','.','#','.','#','.','#','.','#'},
{'#','.','.','.','.','.','.','.','.','#','.','#'},
{'#','#','#','#','#','#','.','#','#','#','.','#'},
{'.','.','.','.','.','.','.','.','#','.','.','.','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#'}
};

Related

C++, "friend class" that's defined in the same header

I'm following this tutorial to create a simple iterator, although they are iterating primitive int, I'm iterating an object type SpamValue.
I have class called SpamValue and another called SpamValueStackIter and they are tightly coupled, because I didn't want to expose a lot of getters, so I made one class SpamValueStackIter a "friend class" in SpamValue header.
#ifndef SPAMSTACK_H
#define SPAMSTACK_H
#include <iostream>
#include "SpamValue.h"
using namespace std;
class SpamStack
{
public:
friend class SpamValueStackIter;
SpamStack(SpamValue** SpamValueItems, int size)
{
_position =-1;
_total_size = size;
_SpamValueItems = new SpamValue*[_total_size];
int i=0;
for (; i<_total_size; i++)
{
this->_SpamValueItems[i] = SpamValueItems[i];
}
}
~SpamStack()
{
if (NULL!=_SpamValueItems)
{
/*delete each*/
int i =0;
for (; i<_total_size; i++)
{
if (NULL!=_SpamValueItems[i])
{
delete _SpamValueItems[i];
}
}
/*delete the array*/
delete [] _SpamValueItems;
}
}
/*push*/
void push(SpamValue* SpamValue)
{
_SpamValueItems[++_position];
}
/*pop*/
SpamValue* pop()
{
return _SpamValueItems[_position--];
}
/*isEmpty*/
bool isEmpty()
{
return (_position == -1);
}
/*getters*/
SpamValue** getSpamValueItems()
{
return this->_SpamValueItems;
}
int getTotalSize()
{
return _total_size;
}
SpamValueStackIter* createIterator()const;
private:
SpamValue** _SpamValueItems;
int _total_size;
int _position;
};
class SpamValueStackIter
{
const SpamStack* _stack;
int _index;
public:
SpamValueStackIter(const SpamStack *s)
{
_stack = s;
}
/*set index position to first item*/
void first()
{
_index = 0;
}
/*set index position to the next item in the iterator*/
void next()
{
_index++;
}
/*is the iteration completed */
bool isDone()
{
return _index == _stack->_position + 1;
}
/* return the current item */
SpamValue* currentItem()
{
return _stack->_SpamValueItems[index];
}
/*create a new iterator*/
SpamValueStackIter* SpamStack::createIterator()const
{
return new SpamValueStackIter(this);
}
};
#endif /* SPAMSTACK_H*/
In the SpamStack.h:, Im getting this error:
SpamStack.h:77:6: error: ‘SpamValueStackIter’ does not name a type
SpamValueStackIter* createIterator()const;
And also:
SpamStack.h:121:52: error: cannot define member function ‘SpamStack::createIterator tor’ within ‘SpamValueStackIter’
SpamValueStackIter* SpamStack::createIterator()const
Why can't SpamStack resolve the "friend class" that's defined in the same header?
After forward declaration, as suggested by others:
#ifndef SPAMSTACK_H
#define SPAMSTACK_H
#include <iostream>
#include "SpamValue.h"
using namespace std;
/*forward declare*/
class SpamValueStackIter;
class SpamStack
{
public:
friend class SpamValueStackIter;
SpamStack(SpamValue** SpamValueItems, int size)
{
_position =-1;
_total_size = size;
_SpamValueItems = new SpamValue*[_total_size];
int i=0;
for (; i<_total_size; i++)
{
this->_SpamValueItems[i] = SpamValueItems[i];
}
}
~SpamStack()
{
if (NULL!=_SpamValueItems)
{
/*delete each*/
int i =0;
for (; i<_total_size; i++)
{
if (NULL!=_SpamValueItems[i])
{
delete _SpamValueItems[i];
}
}
/*delete the array*/
delete [] _SpamValueItems;
}
}
/*push*/
void push(SpamValue* SpamValue)
{
_SpamValueItems[++_position];
}
/*pop*/
SpamValue* pop()
{
return _SpamValueItems[_position--];
}
/*isEmpty*/
bool isEmpty()
{
return (_position == -1);
}
/*getters*/
SpamValue** getSpamValueItems()
{
return this->_SpamValueItems;
}
int getTotalSize()
{
return _total_size;
}
SpamValueStackIter* createIterator()const;
private:
SpamValue** _SpamValueItems;
int _total_size;
int _position;
};
class SpamValueStackIter
{
public:
SpamValueStackIter(const SpamStack *s)
{
_stack = s;
}
/*set index position to first item*/
void first()
{
_index = 0;
}
/*set index position to the next item in the iterator*/
void next()
{
_index++;
}
/*is the iteration completed */
bool isDone()
{
return _index == _stack->_position + 1;
}
/* return the current item */
SpamValue* currentItem()
{
return _stack->_SpamValueItems[index];
}
private:
const SpamStack* _stack;
int _index;
};
/create a new iterator/
SpamValueStackIter* SpamStack::createIterator()const
{
return new SpamValueStackIter(this);
}
#endif /* SPAMSTACK_H */
In getting this error now:
SpamStack.h:117:45: error: invalid types ‘SpamValue** const[<unresolved overloaded function type>]’ for array subscript
return _stack->_SpamValueItems[index];

Non overwriting object

Hello I have four files: AllOnesGA.cpp, GeneticAlgorithm.h, Population.h, Individual.h
And I don't know why individual.getFitness() give me -1 and not 2 that is the last value that I give it with the method setFitness
I simplified my code
int main()
{
GeneticAlgorithm ga(100);
Population population = ga.initPopulation(50);
ga.evalPopulation(population);
ga.isTerminationConditionMet(population);
...
In geneticAlgorithm
void evalPopulation(Population population)
{
double populationFitness = 0;
for (Individual individual : population.getIndividual())
{
individual.setFitness(2);
}
}
bool isTerminationConditionMet(Population population)
{
for(Individual individual :population.getIndividual())
{
cout<<individual.getFitness()<<endl; //this gives -1 and not 2
}
}
and in Individual.h
class Individual{
public:
Individual(vector<int> chromosome2)
:chromosome(chromosome2),chromosomeLength(chromosome2.size())
{}
Individual(int chromosomeLength)
:chromosomeLength(chromosomeLength)
{
for(int gene=0;gene<chromosomeLength;gene++)
{
chromosome.push_back(gene);
}
}
int getChromosomeLength()
{
return chromosomeLength;
}
vector<int> getChromosome()
{
return chromosome;
}
int getGene(int offset)
{
return chromosome[offset];
}
void setFitness(double fitness)
{
this->fitness=fitness;
}
double getFitness()
{
return fitness;
}
private:
vector<int> chromosome;
double fitness=-1.0;
int chromosomeLength;
from Population.h
...
vector <Individual> getIndividual()
{
return this->population;
}
...
private:
vector <Individual> population;
But don't confuse the object population from AllOnesGA.cpp and the population object from Population.h that is a vector.
Any recomendation?

Class constructor does not take input parameters C++ (all attributes get the same value and the same value applies to all objects)

I created a class named Triangle that takes triangles as objects.
The attributes are the ID of the triangle (m_ID), the index of the three vertices (m_S1, m_S2, m_S3) and the index of the three adjacent triangles (m_T1, m_T2, m_T3).
I want to create a triangle object by passing input parameters in the constructor.
The parameters are passed by value, but the object is badly created. In fact, all three triangles I tried to create (triangle2, 3 and 4) have the same value for attributes. The value is -858993460 which looks like an address.
Even when I use the simplest input values (Triangle triangle4(1,1,1,1,1,1,1)), the attributes are not created correctly.
/*
Name: insert
Description: insert the new point and create 3 new triangles
*/
void insert(const int triangleIndex)
{
// Extract the Index of the new point
int newPointIndex = pointList.size();
// Create the line of the second new triangle
Triangle triangle2(triangleList.size() + 1, newPointIndex, triangleList[triangleIndex].getS3(), triangleList[triangleIndex].getS1(), triangleList[triangleIndex].getT2(), triangleList.size() + 2, triangleIndex);
// Create the line of the third new triangle
Triangle triangle3(triangleList.size() + 2, newPointIndex, triangleList[triangleIndex].getS1(), triangleList[triangleIndex].getS2(), triangleList[triangleIndex].getT3(), triangleIndex, triangleList.size() + 1);
// Test only
Triangle triangle4(1, 1, 1, 1, 1, 1, 1);
// Get the adjacent triangles of the base triangle
int T1 = triangleList[triangleIndex].getT1();
int T2 = triangleList[triangleIndex].getT2();
int T3 = triangleList[triangleIndex].getT3();
// Update the line of first new triangle
triangleList[triangleIndex].setS1(newPointIndex);
triangleList[triangleIndex].setT2(triangleList.size() + 1);
triangleList[triangleIndex].setT3(triangleList.size() + 2);
// Update the adjacent triangles
triangleList[T2].setT3(triangleList.size() + 1);
triangleList[T3].setT2(triangleList.size() + 2);
// Insert the new triangles in the triangulation
triangleList.push_back(triangle2);
triangleList.push_back(triangle3);
}
/*-----------------------------------------
Name: Triangle
Author: Michael Landry
Description: Implementation of class Triangle
Date: 2018-02-23
Version: 1.00
-------------------------------------------*/
#include "Triangle.h"
// Constructor with no parameter
Triangle::Triangle() : m_ID(0), m_S1(0), m_S2(0), m_S3(0), m_T1(0), m_T2(0), m_T3(0)
{
}
// Constructor with parameters
Triangle::Triangle(int p_ID, int p_S1, int p_S2, int p_S3, int p_T1, int p_T2, int p_T3) : m_ID(p_ID), m_S1(p_S1), m_S2(p_S2), m_S3(p_S3), m_T1(p_T1), m_T2(p_T2), m_T3(p_T3)
{
}
// Global setter
void Triangle::setTriangle(int p_ID, int p_S1, int p_S2, int p_S3, int p_T1, int p_T2, int p_T3)
{
m_ID = p_ID;
m_S1 = p_S1;
m_S2 = p_S2;
m_S3 = p_S3;
m_T1 = p_T1;
m_T2 = p_T2;
m_T3 = p_T3;
}
// Setter for each individual parameter
void Triangle::setID(int p_ID)
{
m_ID = p_ID;
}
void Triangle::setS1(int p_S1)
{
m_S1 = p_S1;
}
void Triangle::setS2(int p_S2)
{
m_S2 = p_S2;
}
void Triangle::setS3(int p_S3)
{
m_S3 = p_S3;
}
void Triangle::setT1(int p_T1)
{
m_T1 = p_T1;
}
void Triangle::setT2(int p_T2)
{
m_T2 = p_T2;
}
void Triangle::setT3(int p_T3)
{
m_T3 = p_T3;
}
// Getter for each individual parameter
int Triangle::getID() const
{
return m_ID;
}
int Triangle::getS1() const
{
return m_S1;
}
int Triangle::getS2() const
{
return m_S2;
}
int Triangle::getS3() const
{
return m_S3;
}
int Triangle::getT1() const
{
return m_T1;
}
int Triangle::getT2() const
{
return m_T2;
}
int Triangle::getT3() const
{
return m_T3;
}
/*-----------------------------------------
Name: Triangle
Author: Michael Landry
Description: Declaration of class Triangle
Date: 2018-02-23
Version: 1.00
-------------------------------------------*/
#ifndef TRIANGLE_H_
#define TRIANGLE_H_
class Triangle
{
public:
// Constructor with no parameter
Triangle::Triangle();
// Constructor with parameters
Triangle::Triangle(int, int, int, int, int, int, int);
// Setters
void Triangle::setTriangle(int p_ID, int p_S1, int p_S2, int p_S3, int p_T1, int p_T2, int p_T3);
void Triangle::setID(int p_ID);
void Triangle::setS1(int p_S1);
void Triangle::setS2(int p_S2);
void Triangle::setS3(int p_S3);
void Triangle::setT1(int p_T1);
void Triangle::setT2(int p_T1);
void Triangle::setT3(int p_T1);
// Getters
int Triangle::getID() const;
int Triangle::getS1() const;
int Triangle::getS2() const;
int Triangle::getS3() const;
int Triangle::getT1() const;
int Triangle::getT2() const;
int Triangle::getT3() const;
private:
// The ID of the triangle
int m_ID;
// The 3 vertices forming the triangle
int m_S1;
int m_S2;
int m_S3;
// The 3 adjacent triangles
int m_T1;
int m_T2;
int m_T3;
};
#endif /* TRIANGLE_H_ */

Function returning function pointer from table as a parameter

I have been reading for a while, but today I can't figure someting out and find a solution.
How to return a function pointer from a function table as parameter? All similair solutions don't work for this one and end up not compiling.
I have tried a lot of methods but the compiler always returns with errors like:
function returning function is not allowed solution (when using typedef void (*func)();)
As NO parameters have to be passed into the final routine it should be possible.
My simplified example:
void PrintOne(void) { printf("One")};
void PrintTwo(void) { printf("Two")};
struct ScanListStruct
{
int Value;
void (*Routine)(void);
}
const ScanListStruct DoList[] =
{
{1, PrintOne},
{2, PrintTwo}
}
bool GetRoutine(void *Ptr, int Nr)
{
for (int x =0; x<=1; x++)
{
if (DoList[x].Value = Nr)
{
Ptr = DoList[(x)].Routine;
//((*DoList[(x)].Routine)()); // Original Working and executing version!
return true;
}
}
return false;
}
void main(void)
{
int y = 1;
void (*RoutineInMain)(); // Define
if (GetRoutine( RoutineInMain, y) == true) // get the address
{
RoutineInMain(); // Execute the function
}
}
There a few things wrong with the code;
Syntax errors (missing ; etc.)
main must return int
GetRoutine should accept the function pointer by reference, not just a void* pointer to anything
if condition should contain an equality test, not an assignment
As follows, works as expected;
void PrintOne(void) { printf("One"); };
void PrintTwo(void) { printf("Two"); };
struct ScanListStruct
{
int Value;
void (*Routine)(void);
};
const ScanListStruct DoList[] =
{
{1, &PrintOne},
{2, &PrintTwo}
};
bool GetRoutine(void (*&Ptr)(), int Nr)
{
for (int x =0; x<=1; x++)
{
if (DoList[x].Value == Nr)
{
Ptr = *DoList[(x)].Routine;
//((*DoList[(x)].Routine)()); // Original Working and executing version!
return true;
}
}
return false;
}
int main(void)
{
int y = 1;
void (*RoutineInMain)(); // Define
if (GetRoutine( RoutineInMain, y) == true) // get the address
{
RoutineInMain(); // Execute the function
}
}
Prints One.
You have lots of errors in your code. Like here you put the comas at the wrong place:
void PrintOne(void) { printf("One")};
void PrintTwo(void) { printf("Two")};
It should be
void PrintOne(void) { printf("One");}
void PrintTwo(void) { printf("Two");}
And here you are using the wrong operator, = instead of ==.
if (DoList[x].Value = Nr)
When the argument Ptr is a pointer, and that is passed by value, so the value assigned in the function will not be available when the function returns.
This is how your code should be:
void PrintOne(void) { printf("One"); }
void PrintTwo(void) { printf("Two"); }
typedef void(*prototype)();
struct ScanListStruct
{
int Value;
prototype Routine;
};
const ScanListStruct DoList[] =
{
{ 1, PrintOne },
{ 2, PrintTwo }
};
bool GetRoutine(prototype &Ptr, int Nr)
{
for (int x = 0; x <= 1; x++)
{
if (DoList[x].Value == Nr)
{
Ptr = DoList[(x)].Routine;
return true;
}
}
return false;
}
int main()
{
int y = 1;
prototype RoutineInMain; // Define
if (GetRoutine(RoutineInMain, y) == true) // get the address
{
RoutineInMain(); // Execute the function
}
return 0;
}

Including any new header makes the project give irrelevant errors

Im working on an open-source project "powder toy" and manged to compile it under msvc. Run fine. But when I include a new header in air.h, compiling gives errors such as:
'GetUserNameA! is not a member of 'SaveInfo' in GameController.cpp file.
and
syntax error: '::' in BitmapBrush.h file. ---> pointing min - max of windows.
Both files are untouched and does not have anything in common with the header I added:
Aux_cl.h ---> added to beginning of air.h
this header has just opencl headers which powdertoy does not have them in any of other cpp/h files.
What am I doing wrong?
If I delete the inclusion of Aux_cl, project compiles fine even with the opencl bindings in the Aux_cl(this not bind to anything, just compiled separately I think)
Edit: When I add "#undef min" and "#undef GetUserName" before those error points, it compiles file, I wonder how it works without my header files included before.
Edit2: Should I add #def blabla just after the functions after those #undef blabla ?
Here is the
Air.h:
#ifndef AIR_H
#define AIR_H
#include "Config.h"
#include "Aux_cl.h"
class Simulation;
class Air
{
public:
Simulation & sim;
int airMode;
float ambientAirTemp;
Aux_cl *gpu;
//Arrays from the simulation
unsigned char (*bmap)[XRES/CELL];
unsigned char (*emap)[XRES/CELL];
float (*fvx)[XRES/CELL];
float (*fvy)[XRES/CELL];
//
float vx[YRES/CELL][XRES/CELL];
float ovx[YRES/CELL][XRES/CELL];
float vy[YRES/CELL][XRES/CELL];
float ovy[YRES/CELL][XRES/CELL];
float pv[YRES/CELL][XRES/CELL];
float opv[YRES/CELL][XRES/CELL];
float hv[YRES/CELL][XRES/CELL];
float ohv[YRES/CELL][XRES/CELL]; // Ambient Heat
unsigned char bmap_blockair[YRES/CELL][XRES/CELL];
unsigned char bmap_blockairh[YRES/CELL][XRES/CELL];
float kernel[9];
void make_kernel(void);
void update_airh(void);
void update_air(void);
void Clear();
void ClearAirH();
void Invert();
Air(Simulation & sim);
};
#endif
Aux_cl.h:
#pragma once
#define __CL_ENABLE_EXCEPTIONS
#include<CL\opencl.h>
#include<CL\cl.hpp>
#include<CL\cl.h>
#include"ClKernelFactory.h" // this has nothing but string variables and simple function. No other includes about windows or powder toy
#define FIRST_COMPUTE_DEVICE 0
/*
Simple compute helper class for your heavy work loads.
Uses OpenCL 1.2 headers so you may need an up-to-date CPU or an AMD-GPU for now.
Instances of this classes are different contexts and should be compiled serially.
Computation can be concurrent without fear.
Writer: Huseyin Tugrul BUYUKISIK
*/
class Aux_cl
{
public:
cl::Context *context;
std::vector<cl::Program::Sources> sources;
std::vector<cl::Program> programs;
std::vector<std::vector<cl::Device>> devicesCPU;
std::vector<std::vector<cl::Device>> devicesGPU;
cl::CommandQueue cq;
std::vector<cl::Buffer> buffers;
std::vector<cl::Memory> memories;
std::vector<cl::NDRange> globalRanges;
std::vector<cl::NDRange> localRanges;
std::vector<cl::NDRange> referenceRanges;
std::vector<std::string> kernelFunctionsToBeCompiled;
std::vector<std::string> kernelNames;
std::vector<std::string> bufferNames;
std::vector<cl::Kernel> kernelFunctions;
std::vector<std::pair<std::vector<int>,int>> bufferTargetsForKernels; // {500}, {501, 502},{530} ... --> buffer ids
// 0,1,2,... --> kernel ids
std::vector<std::pair<int,std::vector<std::string>>> bufferDefinitions;
std::vector<std::pair<int,std::string>> kernelDefinitions;
std::vector<cl::Platform> platforms;
cl_context_properties *context_properties;
std::string CPU_GPU; //"gpu" "GPU" "graphics" "GRAPHICS" "cpu" "CPU"
int kernelId;
int bufferId;
int numberOfCPU;
int numberOfGPU;
int first_CPU_platform;
int first_CPU_device;
int first_GPU_platform;
int first_GPU_device;
int second_CPU_platform;
int second_CPU_device;
int second_GPU_platform;
int second_GPU_device;
int which_one;
static const int FIRST=0, SECOND=1, THIRD=2, FOURTH=3, OMG=4;
Aux_cl(std::string gpu_or_cpu, int whichOne)
{
which_one=whichOne;
CPU_GPU=gpu_or_cpu;
kernelId=0;
bufferId=0;
numberOfGPU=0;
numberOfCPU=0;
cl::Platform::get(&platforms);
for(int i=0;i<platforms.size();i++)
{
devicesCPU.push_back(std::vector<cl::Device>()); // one device list for each platform (AMD, NVIDIA, INTEL, ...)
}
for(int i=0;i<platforms.size();i++)
{
devicesGPU.push_back(std::vector<cl::Device>()); // one device list for each platform (AMD, NVIDIA, INTEL, ...)
}
//selecting the platform that has a cpu or gpu specified in construction
for(int i=0;i<platforms.size();i++)
{
platforms[i].getDevices(CL_DEVICE_TYPE_CPU,&devicesCPU[i]);
}
//selecting the platform that has a cpu or gpu specified in construction
for(int i=0;i<platforms.size();i++)
{
platforms[i].getDevices(CL_DEVICE_TYPE_GPU,&devicesGPU[i]);
}
//searching device lists for a gpu or cpu (as selected in construction)
for(int i=0;i<platforms.size();i++)
{
bool notFoundYet=true;
bool notFoundYet2=true;
if(devicesCPU[i].size()>0)
for(int j=0;j<devicesCPU[i].size();j++)
{
if(notFoundYet)
{
first_CPU_platform=i;
first_CPU_device=j;
notFoundYet=false;
}
if((!notFoundYet)&&(notFoundYet2))
{
second_CPU_platform=i;
second_CPU_device=j;
notFoundYet2=false;
}
numberOfCPU++;
}
}
//searching device lists for a gpu or cpu (as selected in construction)
for(int i=0;i<platforms.size();i++)
{
bool notFoundYet=true;bool notFoundYet2=true;
if(devicesGPU[i].size()>0)
for(int j=0;j<devicesCPU[i].size();j++)
{
if(notFoundYet)
{
first_GPU_platform=i;
first_GPU_device=j;
notFoundYet=false;
}
if((!notFoundYet)&&(notFoundYet2))
{
second_CPU_platform=i;
second_CPU_device=j;
notFoundYet2=false;
}
numberOfGPU++;
}
}
};
void AddKernelFromFactory(KernelIngredients ki)
{
AddKernelToPool(ki.bodyOfKernel, ki.nameOfKernel,
ki.globalX,ki.globalY,
ki.localX,ki.localY);
}
void AddKernelToPool(std::string KernelItself, std::string KernelName,
int GlobalRangeX,int GlobalRangeY,
int LocalRangeX,int LocalRangeY)
{
if(GlobalRangeY!=0)
{
kernelFunctionsToBeCompiled.push_back(KernelItself);
kernelNames.push_back(KernelName);
globalRanges.push_back(cl::NDRange(GlobalRangeX,GlobalRangeY));
localRanges.push_back(cl::NDRange(LocalRangeX,LocalRangeY));
referenceRanges.push_back(cl::NDRange(0,0));
kernelDefinitions.push_back(std::pair<int,std::string>(kernelId,KernelName));
kernelId++;
}
else
{
kernelFunctionsToBeCompiled.push_back(KernelItself);
kernelNames.push_back(KernelName);
globalRanges.push_back(cl::NDRange(GlobalRangeX));
localRanges.push_back(cl::NDRange(LocalRangeX));
referenceRanges.push_back(cl::NDRange(0));
kernelDefinitions.push_back(std::pair<int,std::string>(kernelId,KernelName));
kernelId++;
}
};
void AddBufferToPool(std::string nameOfBuffer,std::string typeOfBuffer, int sizeOfBuffer)
{
// 0,{"a","float","8192"}
std::vector<std::string> BufferNameTypeSize;
BufferNameTypeSize.push_back(nameOfBuffer);
BufferNameTypeSize.push_back(typeOfBuffer);
BufferNameTypeSize.push_back(std::to_string(sizeOfBuffer));
bufferDefinitions.push_back(std::pair<int,std::vector<std::string>>(bufferId,BufferNameTypeSize));
bufferNames.push_back(BufferNameTypeSize[0]);
bufferId++;
}
void WireBuffersToSingleKernel(std::vector<std::string> buffers, std::string theKernel )
{
std::vector<int> bufferL;
int index=-1;
for(int i=0;i<buffers.size();i++)
{
for(int j=0;j<bufferDefinitions.size();j++)
{
if(bufferDefinitions[j].second[0]==(buffers[i]))
{
bufferL.push_back(bufferDefinitions[j].first);
}
}
for(int k=0;k<kernelDefinitions.size();k++)
{
if(kernelDefinitions[k].second==(theKernel))
{
index=kernelDefinitions[k].first;
}
}
}
bufferTargetsForKernels.push_back(std::pair<std::vector<int>,int>(bufferL,index));
// buffers {23,33,10,1,40} for the kernel {3} as a pair
}
void compile()
{
if(which_one==FIRST)
{
//clGetPlatformIDs(1,&platform_id,NULL);
if((CPU_GPU==("CPU"))||(CPU_GPU==("cpu")))
{
cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[first_CPU_platform])(), 0};
context=new cl::Context(CL_DEVICE_TYPE_CPU, properties);
}
if((CPU_GPU==("GPU"))||(CPU_GPU==("gpu")))
{
cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[first_GPU_platform])(), 0};
context=new cl::Context(CL_DEVICE_TYPE_GPU, properties);
}
}
if(which_one==SECOND)
{
//clGetPlatformIDs(1,&platform_id,NULL);
if((CPU_GPU==("CPU"))||(CPU_GPU==("cpu")))
{
cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[second_CPU_platform])(), 0};
context=new cl::Context(CL_DEVICE_TYPE_CPU, properties);
}
if((CPU_GPU==("GPU"))||(CPU_GPU==("gpu")))
{
cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[second_GPU_platform])(), 0};
context=new cl::Context(CL_DEVICE_TYPE_GPU, properties);
}
}
/*
for(int i=0;i<platforms.size();i++)
{
devices[i]=context[0].getInfo<CL_CONTEXT_DEVICES>(); // device lists are created
}
*/
if(which_one==FIRST)
{
for(int i=0;i<kernelFunctionsToBeCompiled.size();i++)
{
sources.push_back(cl::Program::Sources());
sources[i].push_back(std::make_pair(kernelFunctionsToBeCompiled[i].data(),kernelFunctionsToBeCompiled[i].length()));
programs.push_back(cl::Program(context[0],sources[i]));
if((CPU_GPU==("CPU"))||(CPU_GPU==("cpu")))
{
programs[i].build(devicesCPU[first_CPU_platform]);
}
if((CPU_GPU==("GPU"))||(CPU_GPU==("gpu")))
{
programs[i].build(devicesGPU[first_GPU_platform]);
}
kernelFunctions.push_back(cl::Kernel(programs[i],kernelNames[i].data()));
}
if((CPU_GPU==("CPU"))||(CPU_GPU==("cpu")))
{
cq=cl::CommandQueue(context[0],devicesCPU[first_CPU_platform][first_CPU_device]);
}
if((CPU_GPU==("GPU"))||(CPU_GPU==("gpu")))
{
cq=cl::CommandQueue(context[0],devicesGPU[first_GPU_platform][first_GPU_device]);
}
}
if(which_one==SECOND)
{
for(int i=0;i<kernelFunctionsToBeCompiled.size();i++)
{
sources.push_back(cl::Program::Sources());
sources[i].push_back(std::make_pair(kernelFunctionsToBeCompiled[i].data(),kernelFunctionsToBeCompiled[i].length()));
programs.push_back(cl::Program(context[0],sources[i]));
if((CPU_GPU==("CPU"))||(CPU_GPU==("cpu")))
{
programs[i].build(devicesCPU[second_CPU_platform]);
}
if((CPU_GPU==("GPU"))||(CPU_GPU==("gpu")))
{
programs[i].build(devicesGPU[second_GPU_platform]);
}
kernelFunctions.push_back(cl::Kernel(programs[i],kernelNames[i].data()));
}
if((CPU_GPU==("CPU"))||(CPU_GPU==("cpu")))
{
cq=cl::CommandQueue(context[0],devicesCPU[second_CPU_platform][second_CPU_device]);
}
if((CPU_GPU==("GPU"))||(CPU_GPU==("gpu")))
{
cq=cl::CommandQueue(context[0],devicesGPU[second_GPU_platform][second_GPU_device]);
}
}
for(int i=0;i<bufferDefinitions.size();i++)
{
int sizeOfBuffer=std::stoi(bufferDefinitions[i].second[2]);
if(bufferDefinitions[i].second[1]=="float")
{
buffers.push_back(cl::Buffer(context[0],CL_MEM_READ_WRITE,sizeof(cl_float)*sizeOfBuffer));
}
if(bufferDefinitions[i].second[1]=="int")
{
buffers.push_back(cl::Buffer(context[0],CL_MEM_READ_WRITE,sizeof(cl_int)*sizeOfBuffer));
}
if(bufferDefinitions[i].second[1]=="double")
{
buffers.push_back(cl::Buffer(context[0],CL_MEM_READ_WRITE,sizeof(cl_double)*sizeOfBuffer));
}
if(bufferDefinitions[i].second[1]=="long")
{
buffers.push_back(cl::Buffer(context[0],CL_MEM_READ_WRITE,sizeof(cl_long)*sizeOfBuffer));
}
if(bufferDefinitions[i].second[1]=="char")
{
buffers.push_back(cl::Buffer(context[0],CL_MEM_READ_WRITE,sizeof(cl_char)*sizeOfBuffer));
}
}
for(int i=0;i<bufferTargetsForKernels.size();i++)
{
for(int j=0;j<bufferTargetsForKernels[i].first.size();j++)
{
kernelFunctions[bufferTargetsForKernels[i].second].setArg(j,buffers[bufferTargetsForKernels[i].first[j]]);
}
}
}
void SelectClBuffersToWriteOnThem()
{
}
void SelectClBuffersToReadFromThem()
{
}
void WriteToClFromCPP(std::string clArrayName,float *arrayCPP)
{
for(int i=0;i<buffers.size();i++)
{
if(bufferNames[i]==clArrayName)
{
cq.finish();
cq.enqueueWriteBuffer(buffers[i],CL_TRUE,0,sizeof(cl_float)*(std::atoi(bufferDefinitions[i].second[2].data())),arrayCPP);
cq.finish();
}
}
}
void WriteToClFrom2DCPP(std::string clArrayName,static float *arrayCPP) // 2D array = contiguous?
{
for(int i=0;i<buffers.size();i++)
{
if(bufferNames[i]==clArrayName)
{
cq.finish();
cq.enqueueWriteBuffer(buffers[i],CL_TRUE,0,sizeof(cl_float)*(std::atoi(bufferDefinitions[i].second[2].data())), arrayCPP);
cq.finish();
}
}
}
void WriteToClFrom2DCPP(std::string clArrayName,static char *arrayCPP) // 2D array = contiguous?
{
for(int i=0;i<buffers.size();i++)
{
if(bufferNames[i]==clArrayName)
{
cq.finish();
cq.enqueueWriteBuffer(buffers[i],CL_TRUE,0,sizeof(cl_char)*(std::atoi(bufferDefinitions[i].second[2].data())), arrayCPP);
cq.finish();
}
}
}
void ReadFromClToCPP(std::string clArrayName, float *arrayCPP)
{
for(int i=0;i<buffers.size();i++)
{
if(bufferNames[i]==clArrayName)
{
cq.finish();
cq.enqueueReadBuffer(buffers[i],CL_TRUE,0,sizeof(cl_float)*(std::atoi(bufferDefinitions[i].second[2].data())),arrayCPP);
cq.finish();
}
}
}
void ReadFromClTo2DCPP(std::string clArrayName,static float *arrayCPP) // 2D arrays are contiguous?
{
for(int i=0;i<buffers.size();i++)
{
if(bufferNames[i]==clArrayName)
{
cq.finish();
cq.enqueueReadBuffer(buffers[i],CL_TRUE,0,sizeof(cl_float)*(std::atoi(bufferDefinitions[i].second[2].data())), arrayCPP);
cq.finish();
}
}
}
void ReadFromClTo2DCPP(std::string clArrayName,static char *arrayCPP) // 2D arrays are contiguous?
{
for(int i=0;i<buffers.size();i++)
{
if(bufferNames[i]==clArrayName)
{
cq.finish();
cq.enqueueReadBuffer(buffers[i],CL_TRUE,0,sizeof(cl_char)*(std::atoi(bufferDefinitions[i].second[2].data())), arrayCPP);
cq.finish();
}
}
}
std::vector<int> computeList;
void MakeKernelListToRun(std::vector<std::string> listOfKernels)
{
computeList.clear();
for(int j=0;j<listOfKernels.size();j++)
{
for(int i=0;i<kernelNames.size();i++)
{
if(listOfKernels[j]==kernelNames[i])
{
computeList.push_back(i);
}
}
}
}
void ComputeList()
{
for(int i=0;i<computeList.size();i++)
{
cq.enqueueNDRangeKernel(kernelFunctions[computeList[i]],referenceRanges[computeList[i]],globalRanges[computeList[i]],localRanges[computeList[i]]);
}
}
void Compute(std::string kernelName)
{
for(int i=0;i<kernelNames.size();i++)
{
if(kernelName==kernelNames[i])
{
cq.enqueueNDRangeKernel(kernelFunctions[i],referenceRanges[i],globalRanges[i],localRanges[i]);
}
}
}
void releaseClResources()
{
cq.finish();
// opencl c++ bindings wrapper deletes / releases automatically when of no use
}
~Aux_cl(void)
{
delete context;
};
};
Last edit:
In KernelIngredients.cpp there is only #include "KernelIngredients.h"
In KernelIngredients.h there is no inclusion
In ClKernelFactory.h there is only #include "KernelIngredients.h"
In ClKernelFactory.cpp there is only #include "ClKernelFactory.h"
In Aux_cl.cpp there is only #include "Aux_cl.h"
In Aux_cl.h there is only opencl inclusions (i wish these dont use the windows things that clashes with min() and GetUserName() :S )
In Air.h there are #include "Aux_cl.h" and #include"ClKernelFactory.h"
Looks like one of the headers included indirectly via air.h is defining a macro which conflicts with a symbol name elsewhere in the sources. The macro basically defines a search-replace operation which is carried out by the preprocessor, before the compiler kicks in.
Take this example:
/** header_1.h **/
void foo(){ std::cout << "Hello" << std::endl; }
/** header_2.h **/
#define foo bar
/** main.cpp **/
#include "header_1.h"
#include "header_2.h"
int main() {
foo();
}
Compiling main.cpp will give you an error that the method bar is not defined, since the preprocessor replaced all occurrences of foo appearing after the point header_2.hwas included with bar.