20 GLuint input_buffer= 0;
23 std::vector<int> input;
26 const int logNmax= 26;
32 int o= rand() % 10000;
33 for(
int i= 0; i < (1<<logNmax); i++)
34 input.push_back( o + rand() % 10000);
36 glGenBuffers(1, &input_buffer);
37 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, input_buffer);
38 glBufferData(GL_SHADER_STORAGE_BUFFER,
sizeof(
int) * input.size(), input.data(), GL_STATIC_READ);
40 glGenBuffers(1, &tmp_buffer);
41 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, tmp_buffer);
42 glBufferData(GL_SHADER_STORAGE_BUFFER,
sizeof(
int) * input.size(), input.data(), GL_STREAM_READ);
61 glDeleteBuffers(1, &input_buffer);
62 glDeleteBuffers(1, &tmp_buffer);
67 int main(
int argc,
char **argv )
82 printf(
"[error] init failed.\n");
88 glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &threads_max);
89 printf(
"threads max %d\n", threads_max);
92 GLint size_max[3]= { };
93 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0, &size_max[0]);
94 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1, &size_max[1]);
95 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2, &size_max[2]);
96 printf(
"size max %d %d %d\n", size_max[0], size_max[1], size_max[2]);
99 GLint groups_max[3]= { };
100 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 0, &groups_max[0]);
101 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 1, &groups_max[1]);
102 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 2, &groups_max[2]);
103 printf(
"groups max %d %d %d\n", groups_max[0], groups_max[1], groups_max[2]);
107 glGetIntegerv(GL_MAX_COMPUTE_SHARED_MEMORY_SIZE, &size);
108 printf(
"shared memory %dKb\n", size / 1024);
112 FILE *out= fopen(
"min_data.txt",
"wt");
114 FILE *out= fopen(
"min_atomic.txt",
"wt");
120 for(
int logN= 1; logN < logNmax; logN++)
125 glUseProgram(program);
128 std::chrono::high_resolution_clock::time_point start= std::chrono::high_resolution_clock::now();
135 glDispatchCompute((N+1023) / 1024, 1, 1);
139 for(
int n= N/2; n > 1; n= n/2)
144 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
147 glDispatchCompute((n+1023) / 1024, 1, 1);
154 for(
int i=0 ; i < 100; i++)
156 glDispatchCompute((N+1023) / 1024, 1, 1);
162 glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
164 std::chrono::high_resolution_clock::time_point stop= std::chrono::high_resolution_clock::now();
165 long int gpu_time= std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count();
167 std::vector<int> tmp(input.size(), 0);
168 glBindBuffer(GL_SHADER_STORAGE_BUFFER, tmp_buffer);
169 glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0,
sizeof(
int) * tmp.size(), tmp.data());
171 printf(
"gpu min %d:", tmp[1]);
173 printf(
" %03dus\n", (
int) (gpu_time / 1000));
177 std::chrono::high_resolution_clock::time_point start= std::chrono::high_resolution_clock::now();
181 for(
int i= 1; i < N; i++)
184 std::chrono::high_resolution_clock::time_point stop= std::chrono::high_resolution_clock::now();
185 long int cpu_time= std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count();
188 printf(
" %02dms %03dus\n", (
int) (cpu_time / 1000000), (
int) ((cpu_time / 1000) % 1000));
191 fprintf(out,
"%d %f %f\n", N, gpu_time / 1000.f, cpu_time / 1000.f);
Context create_context(Window window)
cree et configure un contexte opengl
void release_window(Window window)
destruction de la fenetre.
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Window create_window(const int w, const int h, const int major, const int minor, const int samples)
creation d'une fenetre pour l'application.
void release_context(Context context)
detruit le contexte openGL.
Point min(const Point &a, const Point &b)
renvoie la plus petite composante de chaque point. x, y, z= min(a.x, b.x), min(a.y,...
GLuint read_program(const char *filename, const char *definitions)
void program_uniform(const GLuint program, const char *uniform, const std::vector< unsigned > &v)
affecte un tableau de valeurs a un uniform du shader program.
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
int release_program(const GLuint program)
detruit les shaders et le program.
int init(std::vector< const char * > &options)