chore: init
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
// Attention : Extension .cu
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "cudas.h"
|
||||
#include "GM.h"
|
||||
#include "Kernel.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Private *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
__global__ static void kaddScalar(int a , int b , int* ptrSumGM);
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Host *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
__host__ int addScalar_procedurale(int a , int b) // __host__ facultatif
|
||||
{
|
||||
int sum; // variable pour le resultat cite host
|
||||
int* ptrSum = ∑ // on host (CPU)
|
||||
int* ptrSumGM; // on device (GPU)
|
||||
|
||||
// MM (memory managment)
|
||||
size_t size = sizeof(int); // [octet]
|
||||
GM::malloc(&ptrSumGM, size); // Device memory allocation (*)
|
||||
|
||||
// Grid : Specifier number thread : ici 1 thread au total !
|
||||
dim3 dg(1, 1, 1);
|
||||
dim3 db(1, 1, 1);
|
||||
|
||||
kaddScalar<<<dg,db>>>(a,b,ptrSumGM); // assynchrone, call le kernel GPU addScalar
|
||||
|
||||
Kernel::synchronize(); // inutile
|
||||
|
||||
// MM (memory management)
|
||||
{
|
||||
GM::memcpyDToH(ptrSum, ptrSumGM, size); // Device -> Host, MM = barrier de synchronisation
|
||||
GM::free(ptrSumGM); // free memory create in (*)
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Device *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
/**
|
||||
* Hyp : 1 seul thread, ie dg(1,1,1) et db (1,1,1)
|
||||
*/
|
||||
__global__ void kaddScalar(int a , int b , int* ptrSumGM)
|
||||
{
|
||||
*ptrSumGM = a + b; // Hyp: 1 seul thread (pas besoin de plus, pour additioner 2 nombre)
|
||||
|
||||
// debug (1 seule thread ici)
|
||||
printf("[Hello : Device side : addScalar procedurale] %d + %d = %d", a, b, *ptrSumGM);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
// Attention : Extension .cpp
|
||||
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
|
||||
#include "AddScalar.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Imported *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
extern int addScalar_procedurale(int a , int b) ;
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
bool exemple_addScalar_procedurale()
|
||||
{
|
||||
int a = 10;
|
||||
int b = 1;
|
||||
|
||||
int sum = addScalar_procedurale(a, b); // by cuda
|
||||
|
||||
cout << "\n[Hello : Host side : addScalar procedurale] " << a << " + " << b << " = " << sum << endl;
|
||||
|
||||
return sum == a + b;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
26
Student_Cuda_Warmup/src/core/00_addScalar/01_poo/device/addScalar_device.cu
Executable file
26
Student_Cuda_Warmup/src/core/00_addScalar/01_poo/device/addScalar_device.cu
Executable file
@@ -0,0 +1,26 @@
|
||||
// Attention : Extension .cu
|
||||
|
||||
#include "cudas.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Hyp : 1 seul thread, ie dg(1,1,1) et db (1,1,1)
|
||||
*/
|
||||
__global__ void addScalar(float a , float b , float* ptrSumGM)
|
||||
{
|
||||
*ptrSumGM = a + b;
|
||||
|
||||
// debug (1 seule thread ici)
|
||||
printf("\n[Hello : Device side : addScalar object] %.0f + %.0f = %.0f", a, b, *ptrSumGM);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
69
Student_Cuda_Warmup/src/core/00_addScalar/01_poo/host/AddScalar.cu
Executable file
69
Student_Cuda_Warmup/src/core/00_addScalar/01_poo/host/AddScalar.cu
Executable file
@@ -0,0 +1,69 @@
|
||||
// Attention : Extension .cu
|
||||
|
||||
#include "AddScalar.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
#include "GM.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Imported *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
extern __global__ void addScalar(float a, float b, float* ptrSumGM);
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* ptrSum receptionne le resultat
|
||||
*/
|
||||
AddScalar::AddScalar(float a , float b , float* ptrSum) :
|
||||
a(a),//
|
||||
b(b), //
|
||||
ptrSum(ptrSum)
|
||||
{
|
||||
this->sizeFloat = sizeof(float); // [octet]
|
||||
|
||||
// MM
|
||||
{
|
||||
GM::malloc(&ptrSumGM, sizeFloat);
|
||||
}
|
||||
}
|
||||
|
||||
AddScalar::~AddScalar()
|
||||
{
|
||||
//MM
|
||||
{
|
||||
GM::free(ptrSumGM);
|
||||
}
|
||||
}
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Methode *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
void AddScalar::run()
|
||||
{
|
||||
// Grid : specifier le nombre de thread
|
||||
dim3 dg(1, 1, 1);
|
||||
dim3 db(1, 1, 1); // contrainte produit <=1024
|
||||
assert(dg.x * dg.y * dg.z * db.x * db.y * db.z == 1);// 1 seul thread suffit, ici
|
||||
|
||||
addScalar<<<dg,db>>>(a, b, ptrSumGM); // assynchrone
|
||||
|
||||
//Device::synchronize(); // inutile
|
||||
|
||||
// MM (Device -> Host)
|
||||
{
|
||||
GM::memcpyDToH(ptrSum, ptrSumGM, sizeFloat); // MM = barriere synchronisation implicite
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
53
Student_Cuda_Warmup/src/core/00_addScalar/01_poo/host/AddScalar.h
Executable file
53
Student_Cuda_Warmup/src/core/00_addScalar/01_poo/host/AddScalar.h
Executable file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include "cudas.h"
|
||||
#include "Grid.h"
|
||||
|
||||
/**
|
||||
* On passse la grille à AddScalar pour pouvoir facilement la faire varier de l'extérieur pour trouver l'optimum, ou faire des tests avec des grilles différentes
|
||||
*/
|
||||
class AddScalar
|
||||
{
|
||||
/*--------------------------------------*\
|
||||
|* Constructor *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* ptrSum receptionne le resultat a+b
|
||||
*/
|
||||
AddScalar(float a, float b, float* ptrSum);
|
||||
|
||||
virtual ~AddScalar();
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Methodes *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
public:
|
||||
|
||||
void run();
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Attributs *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
private:
|
||||
|
||||
// Inputs
|
||||
float a;
|
||||
float b;
|
||||
|
||||
// Inputs/Outputs
|
||||
float* ptrSum;
|
||||
|
||||
// Tools
|
||||
float* ptrSumGM;
|
||||
size_t sizeFloat;
|
||||
|
||||
};
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
35
Student_Cuda_Warmup/src/core/00_addScalar/01_poo/useAddScalarObject.cpp
Executable file
35
Student_Cuda_Warmup/src/core/00_addScalar/01_poo/useAddScalarObject.cpp
Executable file
@@ -0,0 +1,35 @@
|
||||
// Attention : Extension .cpp
|
||||
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
|
||||
#include "AddScalar.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
bool exemple_addScalar_object()
|
||||
{
|
||||
float a = 8;
|
||||
float b = 80;
|
||||
float sum;
|
||||
|
||||
// Cuda
|
||||
{
|
||||
AddScalar addcuda(a, b, &sum);
|
||||
addcuda.run();
|
||||
}
|
||||
|
||||
cout << "\n[Hello : Host side : addScalar object] " << a << " + " << b << " = " << sum << endl;
|
||||
|
||||
return sum == a + b;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
// Attention : Extension .cu
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "Thread2D.cu.h"
|
||||
#include "Thread1D.cu.h"
|
||||
#include "cudas.h"
|
||||
#include "GM.h"
|
||||
#include "Kernel.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Private *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
static __global__ void kaddArray(float* ptrGMV1 , float* ptrGMV2 , float* ptrGMW , int n);
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Host *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
/**
|
||||
* si possible toujours en float sur un gpu
|
||||
* ptrW receptionne le resultat
|
||||
* n nombre de case
|
||||
*/
|
||||
__host__ bool addArray_procedurale(float* ptrV1 , float* ptrV2 , float* ptrW , int n) // __host__ facultatif
|
||||
{
|
||||
// TODO addArray
|
||||
}
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Device *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
/**
|
||||
* output : void required, because kernel is asynchrone
|
||||
*/
|
||||
__global__ void kaddArray(float* ptrGMV1 , float* ptrGMV2 , float* ptrGMW , int n)
|
||||
{
|
||||
const int NB_THREAD = Thread2D::nbThread();
|
||||
const int TID = Thread2D::tid();
|
||||
|
||||
// pattern entrelacement
|
||||
|
||||
// TODO addArray
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Attention : Extension .cpp
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ArrayTools.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* imported *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
void addArray_procedurale(float* ptrV1 , float* ptrV2 , float* ptrW , int n);
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
bool exemple_addArray_procedurale()
|
||||
{
|
||||
// Exemple de data
|
||||
int n = 8;
|
||||
float* ptrV1 = ArrayTools::createV1(n); // create and fill
|
||||
float* ptrV2 = ArrayTools::createV2(n); // create and fill
|
||||
float* ptrW = new float[n];
|
||||
|
||||
addArray_procedurale(ptrV1, ptrV2, ptrW, n); // by cuda
|
||||
|
||||
// Check justesse
|
||||
bool isOk = ArrayTools::isAddVector_Ok(ptrV1, ptrV2, ptrW, n);
|
||||
|
||||
// Print
|
||||
{
|
||||
ArrayTools::print(ptrV1, n);
|
||||
ArrayTools::print(ptrV2, n);
|
||||
cout << "--------------------------------------------------------------------" << endl;
|
||||
ArrayTools::print(ptrW, n);
|
||||
}
|
||||
|
||||
// Delete
|
||||
{
|
||||
delete ptrV1;
|
||||
delete ptrV1;
|
||||
delete ptrW;
|
||||
}
|
||||
|
||||
return isOk;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "Thread2D.cu.h"
|
||||
#include "Thread1D.cu.h"
|
||||
#include "cudas.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* output : void required, because kernel is asynchrone
|
||||
*/
|
||||
__global__ void addArray(float* ptrGMV1 , float* ptrGMV2 , float* ptrGMW , int n)
|
||||
{
|
||||
const int NB_THREAD = Thread2D::nbThread();
|
||||
const int TID = Thread2D::tid();
|
||||
|
||||
// Debug, facultatif
|
||||
// if (TID == 0)
|
||||
// {
|
||||
// printf("Coucou from device tid = %d", TID);
|
||||
// }
|
||||
|
||||
// pattern entrelacement
|
||||
|
||||
// TODO addArray
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#include "AddArray.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
|
||||
#include "Kernel.h"
|
||||
#include "GM.h"
|
||||
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::to_string;
|
||||
using std::string;
|
||||
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Imported *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
extern __global__ void addArray(float* ptrGMV1 , float* ptrGMV2 , float* ptrGMW , int n);
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Constructeur *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
AddArray::AddArray(const Grid& grid , float* ptrV1 , float* ptrV2 , float* ptrW , int n) :
|
||||
ptrV1(ptrV1), //
|
||||
ptrV2(ptrV2), //
|
||||
ptrW(ptrW), //
|
||||
n(n), //
|
||||
dg(grid.dg), //
|
||||
db(grid.db)
|
||||
{
|
||||
this->sizeVector = -1; // TODO addArray // octet
|
||||
|
||||
// MM (malloc Device)
|
||||
{
|
||||
GM::malloc(&ptrGMV1, sizeVector);
|
||||
// TODO addArray
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
AddArray::~AddArray()
|
||||
{
|
||||
//MM (device free)
|
||||
{
|
||||
GM::free(ptrGMV1);
|
||||
// TODO addArray
|
||||
}
|
||||
}
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Methode *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
/**
|
||||
* override
|
||||
*/
|
||||
void AddArray::run()
|
||||
{
|
||||
// MM (copy Host->Device)
|
||||
{
|
||||
GM::memcpyHToD(ptrGMV1, ptrV1, sizeVector);
|
||||
// TODO addArray
|
||||
}
|
||||
|
||||
// TODO addArray // call kernel // assynchrone
|
||||
|
||||
//Kernel::synchronize();// inutile
|
||||
|
||||
// MM (Device -> Host)
|
||||
{
|
||||
// TODO addArray // MM barier de synchronisation implicite
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "cudas.h"
|
||||
#include "Grid.h"
|
||||
#include "RunnableGPU.h"
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Declaration *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
|
||||
class AddArray
|
||||
{
|
||||
/*--------------------------------------*\
|
||||
|* Constructor *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* update w by v1+v2
|
||||
*/
|
||||
AddArray(const Grid& grid , float* ptrV1 , float* ptrV2 , float* ptrW , int n);
|
||||
|
||||
virtual ~AddArray();
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Methodes *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
public:
|
||||
|
||||
void run();
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Attributs *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
private:
|
||||
|
||||
// Inputs
|
||||
float* ptrV1;
|
||||
float* ptrV2;
|
||||
int n;
|
||||
dim3 dg;
|
||||
dim3 db;
|
||||
|
||||
// Inputs/Outputs
|
||||
float* ptrW;
|
||||
|
||||
// Tools
|
||||
float* ptrGMV1; // dev comme device
|
||||
float* ptrGMV2;
|
||||
float* ptrGMW;
|
||||
size_t sizeVector; //[octet]
|
||||
|
||||
};
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
@@ -0,0 +1,88 @@
|
||||
// Attention : Extension .cpp
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "AddArray.h"
|
||||
#include "ArrayTools.h"
|
||||
#include "Couts.h"
|
||||
#include "Hardware.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/*-------------------------------------*\
|
||||
|* Private *|
|
||||
\*------------------------------------*/
|
||||
|
||||
static Grid createGrid();
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
bool exemple_addArray_object()
|
||||
{
|
||||
// Exemple de data
|
||||
int n = 8;
|
||||
float* ptrV1 = ArrayTools::createV1(n); // create and fill
|
||||
float* ptrV2 = ArrayTools::createV2(n); // create and fill
|
||||
float* ptrW = new float[n];
|
||||
|
||||
// Cuda
|
||||
{
|
||||
Grid grid = createGrid();
|
||||
AddArray cudaCode(grid, ptrV1, ptrV2, ptrW, n);
|
||||
cudaCode.run();
|
||||
}
|
||||
|
||||
// Check justesse
|
||||
bool isOk = ArrayTools::isAddVector_Ok(ptrV1, ptrV2, ptrW, n);
|
||||
|
||||
// Print
|
||||
{
|
||||
ArrayTools::print(ptrV1, n);
|
||||
ArrayTools::print(ptrV2, n);
|
||||
cout << "--------------------------------------------------------------------" << endl;
|
||||
ArrayTools::print(ptrW, n);
|
||||
}
|
||||
|
||||
// Delete
|
||||
{
|
||||
delete ptrV1;
|
||||
delete ptrV1;
|
||||
delete ptrW;
|
||||
}
|
||||
|
||||
return isOk;
|
||||
}
|
||||
|
||||
/*-------------------------------------*\
|
||||
|* Private *|
|
||||
\*------------------------------------*/
|
||||
|
||||
/**
|
||||
* static
|
||||
*/
|
||||
static Grid createGrid()
|
||||
{
|
||||
const int MP = Hardware::getMPCount();
|
||||
const int CORE_MP = Hardware::getCoreCountMP();
|
||||
|
||||
dim3 dg(1, 1, 1); // TODO addArray
|
||||
dim3 db(1, 1, 1); // TODO addArray // produit <=1024
|
||||
Grid grid(dg, db);
|
||||
|
||||
// to remove once coded
|
||||
{
|
||||
Couts::redln("aie aie aie, your best grid won t build itself");
|
||||
assert(false);
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "Thread2D.cu.h"
|
||||
#include "Thread1D.cu.h"
|
||||
#include "cudas.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* Output :
|
||||
*
|
||||
* void required, car kernel is asynchrone!
|
||||
*
|
||||
* Contrainte:
|
||||
* pattern 1<-->1
|
||||
* On associe un thread a chaque case du tableau
|
||||
* </pre>
|
||||
*/
|
||||
__global__ void addArray11(float* ptrGMV1 , float* ptrGMV2 , float* ptrGMW , int n)
|
||||
{
|
||||
const int TID = Thread2D::tid();
|
||||
|
||||
// TODO addArray11
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "AddArray11.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
|
||||
#include "Kernel.h"
|
||||
#include "GM.h"
|
||||
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::to_string;
|
||||
using std::string;
|
||||
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Imported *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
extern __global__ void addArray11(float* ptrGMV1 , float* ptrGMV2 , float* ptrGMW , int n);
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Constructeur *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
AddArray11::AddArray11(const Grid& grid , float* ptrV1 , float* ptrV2 , float* ptrW , int n) :
|
||||
ptrV1(ptrV1), //
|
||||
ptrV2(ptrV2), //
|
||||
ptrW(ptrW), //
|
||||
n(n), //
|
||||
dg(grid.dg), //
|
||||
db(grid.db)
|
||||
{
|
||||
this->sizeVector = -1; // TODO addArray11 // octet
|
||||
|
||||
// TODO addArray11
|
||||
}
|
||||
|
||||
AddArray11::~AddArray11()
|
||||
{
|
||||
// TODO addArray11
|
||||
}
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Methode *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
/**
|
||||
* override
|
||||
*/
|
||||
void AddArray11::run()
|
||||
{
|
||||
// MM (copy Host->Device)
|
||||
{
|
||||
// TODO addArray11
|
||||
}
|
||||
|
||||
assert(dg.x * dg.y * dg.z * db.x * db.y * db.z == n);
|
||||
// TODO addArray11 call kernel
|
||||
|
||||
// MM (Device -> Host)
|
||||
{
|
||||
// TODO addArray11
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "cudas.h"
|
||||
#include "Grid.h"
|
||||
#include "RunnableGPU.h"
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Declaration *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
|
||||
class AddArray11
|
||||
{
|
||||
/*--------------------------------------*\
|
||||
|* Constructor *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* update w by v1+v2
|
||||
*/
|
||||
AddArray11(const Grid& grid , float* ptrV1 , float* ptrV2 , float* ptrW , int n);
|
||||
|
||||
virtual ~AddArray11();
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Methodes *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
public:
|
||||
|
||||
void run();
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Attributs *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
private:
|
||||
|
||||
// Inputs
|
||||
float* ptrV1;
|
||||
float* ptrV2;
|
||||
int n;
|
||||
dim3 dg;
|
||||
dim3 db;
|
||||
|
||||
// Inputs/Outputs
|
||||
float* ptrW;
|
||||
|
||||
// Tools
|
||||
float* ptrGMV1;
|
||||
float* ptrGMV2;
|
||||
float* ptrGMW;
|
||||
size_t sizeVector; //[octet]
|
||||
|
||||
};
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
@@ -0,0 +1,90 @@
|
||||
// Attention : Extension .cpp
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "AddArray11.h"
|
||||
#include "ArrayTools.h"
|
||||
#include "Couts.h"
|
||||
#include "Hardware.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/*-------------------------------------*\
|
||||
|* Private *|
|
||||
\*------------------------------------*/
|
||||
|
||||
static Grid createGrid(int n);
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
bool exemple_addArray_11()
|
||||
{
|
||||
// Exemple de data
|
||||
int n = 8;
|
||||
float* ptrV1 = ArrayTools::createV1(n); // create and fill
|
||||
float* ptrV2 = ArrayTools::createV2(n); // create and fill
|
||||
float* ptrW = new float[n];
|
||||
|
||||
// Cuda
|
||||
{
|
||||
Grid grid = createGrid(n);
|
||||
AddArray11 cudaCode(grid, ptrV1, ptrV2, ptrW, n);
|
||||
cudaCode.run();
|
||||
}
|
||||
|
||||
// Check justesse
|
||||
bool isOk = ArrayTools::isAddVector_Ok(ptrV1, ptrV2, ptrW, n);
|
||||
|
||||
// Print
|
||||
{
|
||||
ArrayTools::print(ptrV1, n);
|
||||
ArrayTools::print(ptrV2, n);
|
||||
cout << "--------------------------------------------------------------------" << endl;
|
||||
ArrayTools::print(ptrW, n);
|
||||
}
|
||||
|
||||
// Delete
|
||||
{
|
||||
delete ptrV1;
|
||||
delete ptrV1;
|
||||
delete ptrW;
|
||||
}
|
||||
|
||||
return isOk;
|
||||
}
|
||||
|
||||
/*-------------------------------------*\
|
||||
|* Private *|
|
||||
\*------------------------------------*/
|
||||
|
||||
/**
|
||||
* static
|
||||
*/
|
||||
static Grid createGrid(int n)
|
||||
{
|
||||
const int MP = Hardware::getMPCount();
|
||||
const int CORE_MP = Hardware::getCoreCountMP();
|
||||
|
||||
dim3 dg(1, 1, 1); // TODO addArray
|
||||
dim3 db(1, 1, 1); // TODO addArray // produit <=1024
|
||||
Grid grid(dg, db);
|
||||
|
||||
// to remove once coded
|
||||
{
|
||||
Couts::redln("aie aie aie, your best grid won t build itself");
|
||||
assert(false);
|
||||
}
|
||||
|
||||
assert(grid.threadCounts() == n);
|
||||
|
||||
return grid;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
113
Student_Cuda_Warmup/src/core/01_addArray/99_tools/ArrayTools.cpp
Executable file
113
Student_Cuda_Warmup/src/core/01_addArray/99_tools/ArrayTools.cpp
Executable file
@@ -0,0 +1,113 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
#include "ArrayTools.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
|
||||
/**
|
||||
* static
|
||||
*/
|
||||
bool ArrayTools::isAddVector_Ok(float* ptrV1 , float* ptrV2 , float* ptrW , int n)
|
||||
{
|
||||
float* ptrResult = new float[n];
|
||||
|
||||
addVectorCPU(ptrV1, ptrV2, ptrResult, n);
|
||||
bool isOk = isEquals(ptrResult, ptrW, n, 1e-6); // comparer version cpu (ptrResult) avec version gpu (ptrW)
|
||||
|
||||
free(ptrResult);
|
||||
|
||||
return isOk;
|
||||
}
|
||||
|
||||
/**
|
||||
* static
|
||||
* 1 2 3 4 5
|
||||
*/
|
||||
float* ArrayTools::createV1(int n)
|
||||
{
|
||||
float* ptrV = new float[n];
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
ptrV[i] = i + 1;
|
||||
}
|
||||
|
||||
return ptrV;
|
||||
}
|
||||
|
||||
/**
|
||||
* static
|
||||
* 10 20 30 40 50
|
||||
*/
|
||||
float* ArrayTools::createV2(int n)
|
||||
{
|
||||
float* ptrV = new float[n];
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
ptrV[i] = (i + 1) * 10;
|
||||
}
|
||||
|
||||
return ptrV;
|
||||
}
|
||||
|
||||
/**
|
||||
* static
|
||||
*/
|
||||
void ArrayTools::print(float* ptrV , int n)
|
||||
{
|
||||
cout << endl;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << ptrV[i] << "\t";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
/*--------------------------------------*\
|
||||
|* Methode private (static) *|
|
||||
\*-------------------------------------*/
|
||||
|
||||
/**
|
||||
* v1,v2,w same size
|
||||
*/
|
||||
void ArrayTools::addVectorCPU(float* ptrV1 , float* ptrV2 , float* ptrW , int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
ptrW[i] = ptrV1[i] + ptrV2[i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* simple
|
||||
*/
|
||||
bool ArrayTools::isEquals(float* ptrV1 , float* ptrV2 , int n , float epsilon)
|
||||
{
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
float delta = fabs(*ptrV1 - *ptrV2);
|
||||
|
||||
if (delta > epsilon)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ptrV1++;
|
||||
ptrV2++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
29
Student_Cuda_Warmup/src/core/01_addArray/99_tools/ArrayTools.h
Executable file
29
Student_Cuda_Warmup/src/core/01_addArray/99_tools/ArrayTools.h
Executable file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Declaration *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
class ArrayTools
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
static bool isAddVector_Ok(float* ptrV1 , float* ptrV2 , float* ptrW , int n);
|
||||
|
||||
static float* createV1(int n);
|
||||
static float* createV2(int n);
|
||||
|
||||
static void print(float* ptrV , int n);
|
||||
|
||||
private:
|
||||
|
||||
static void addVectorCPU(float* ptrV1 , float* ptrV2 , float* ptrW , int n);
|
||||
|
||||
static bool isEquals(float* ptrV1 , float* ptrV2 , int n , float epsilon);
|
||||
|
||||
};
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
65
Student_Cuda_Warmup/src/main/main.cpp
Executable file
65
Student_Cuda_Warmup/src/main/main.cpp
Executable file
@@ -0,0 +1,65 @@
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "Couts.h"
|
||||
|
||||
using std::cerr;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
// Scalar
|
||||
extern bool exemple_addScalar_procedurale();
|
||||
extern bool exemple_addScalar_object();
|
||||
|
||||
// Array
|
||||
extern bool exemple_addArray_procedurale();
|
||||
extern bool exemple_addArray_object();
|
||||
extern bool exemple_addArray_11();
|
||||
|
||||
/*-------------------------------------*\
|
||||
|* Private *|
|
||||
\*------------------------------------*/
|
||||
|
||||
static void array(bool& isOk);
|
||||
static void scalar(bool& isOk);
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* Implementation *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
int main(int argc , char** argv)
|
||||
{
|
||||
const int IS_VERBOSE = true;
|
||||
|
||||
bool isOk = true;
|
||||
|
||||
// Commenter ce dont vous n'avez pas besoin ci-dessous
|
||||
scalar(isOk); // commenter dans la methode ci-dessous ce que vous ne voulez pas lancer
|
||||
array(isOk); // commenter dans la methode ci-dessous ce que vous ne voulez pas lancer
|
||||
|
||||
Couts::statusln(isOk);
|
||||
|
||||
return isOk ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/*-------------------------------------*\
|
||||
|* Private *|
|
||||
\*------------------------------------*/
|
||||
|
||||
static void scalar(bool& isOk)
|
||||
{
|
||||
isOk &= exemple_addScalar_procedurale();
|
||||
isOk &= exemple_addScalar_object();
|
||||
}
|
||||
|
||||
static void array(bool& isOk)
|
||||
{
|
||||
isOk &= exemple_addArray_procedurale();
|
||||
isOk &= exemple_addArray_object();
|
||||
isOk &= exemple_addArray_11();
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*\
|
||||
|* End *|
|
||||
\*---------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user