<commit_before>#include <core_api/environment.h>
#include <core_api/imagehandler.h>
#include <string>
#include <time.h>
#include <stdio.h>
#include "cl_util.h"

using namespace yafaray;

renderEnvironment_t *env;
imageHandler_t *handler;

unsigned int w = 512;
unsigned int h = 384;
float MinRe = -2.0;
float MaxRe = 1.0;
float MinIm = -1.2;
float MaxIm = MinIm+(MaxRe-MinRe)*h/w;
float Re_factor = (MaxRe-MinRe)/(w-1);
float Im_factor = (MaxIm-MinIm)/(h-1);
unsigned MaxIterations = 60000;

void putN(int x, int y, int n) {
	if(n == MaxIterations) {
		handler->putPixel(x,y, color_t(0,0,0));
	} else {
		if(n < MaxIterations/2) {
			float c = n*255/(float)(MaxIterations/2-1);
			handler->putPixel(x,y, color_t(c, 0,0));
		} else {
			float c = (n-MaxIterations/2)*255/(float)(MaxIterations/2);
			handler->putPixel(x, y, color_t(255, c, c));
		}
	}
}

void doCPU() {
	// from http://warp.povusers.org/Mandelbrot/
	for(unsigned y=0; y<h; ++y)
	{
		double c_im = MaxIm - y*Im_factor;
		for(unsigned x=0; x<w; ++x)
		{
			double c_re = MinRe + x*Re_factor;

			double Z_re = c_re, Z_im = c_im;
			int n;
			for(n=0; n<MaxIterations; ++n)
			{
				double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
				if(Z_re2 + Z_im2 > 4)
					break;
				Z_im = 2*Z_re*Z_im + c_im;
				Z_re = Z_re2 - Z_im2 + c_re;
			}
			putN(x,y,n);
		}
		printf("+");
	}
}

void runOCL( void (*cb)(CLDevice device, CLContext *context, CLCommandQueue *queue) ) {
	CLError err;
	CLMain cl;
	std::list<CLPlatform> platforms = cl.getPlatforms(&err);
	checkErr(err || platforms.empty(), "failed to find platforms\n");
	CLPlatform platform = *platforms.begin();

	std::list<CLDevice> devices = platform.getDevices(&err);
	checkErr(err || devices.empty(), "failed to find devices for the chosen platform");
	CLDevice device = *devices.begin();

	CLContext *context = platform.createContext(device, &err);
	checkErr(err || context == NULL, "failed to get a context for the chosen device\n");

	CLCommandQueue *queue = context->createCommandQueue(device, &err);
	checkErr(err || queue == NULL, "failed to create command queue");

	cb(device, context, queue);
	
	queue->free(&err);
	checkErr(err, "failed to free queue");
	context->free(&err);
	checkErr(err, "failed to free context");
}

const char *kernel_source = CL_SRC(
   __kernel void mandelbrot(
		__global int* a
	)
	{
		int x = get_global_id(0);
		int y = get_global_id(1);
		if (x >= ct.w || y >= ct.h)
			return;

		float MaxIm = ct.MinIm + (ct.MaxRe - ct.MinRe) * ct.h / ct.w;
		float Re_factor = (ct.MaxRe - ct.MinRe) / (ct.w - 1);
		float Im_factor = (MaxIm - ct.MinIm) / (ct.h - 1);
		

		float c_im = MaxIm - y*Im_factor;
		float c_re = ct.MinRe + x*Re_factor;

		float Z_re = c_re;
		float Z_im = c_im;

		int n;
		for(n = 0; n < ct.MaxIterations; ++n)
		{
			float Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
			if(Z_re2 + Z_im2 > 4)
				break;
			Z_im = 2*Z_re*Z_im + c_im;
			Z_re = Z_re2 - Z_im2 + c_re;
		}

		a[x + y * ct.w] = n;
	}
);

void doOpenCL(CLDevice device, CLContext *context, CLCommandQueue *queue) {
	CLError err;
	int *a = new int[w*h];

	CLBuffer *buf_a = context->createBuffer(CL_MEM_READ_WRITE, w*h*sizeof(int), NULL, &err);
	checkErr(err, "failed to create buffer");

	CLSrcConst ct;
	ct["w"] = w;
	ct["h"] = h;
	ct["MinRe"] = MinRe;
	ct["MaxRe"] = MaxRe;
	ct["MinIm"] = MinIm;
	ct["MaxIterations"] = MaxIterations;

	std::string src = (std::string)ct + kernel_source;

	CLProgram *program = buildCLProgram(src.c_str(), context, device);

	CLKernel *kernel = program->createKernel("mandelbrot", &err);
	checkErr(err, "failed to create kernel");
	
	kernel->setArgs(buf_a, &err);
	checkErr(err, "failed to set args");<commit_msg>Move the constants into local memory for each kernel instance<commit_after>#include <core_api/environment.h>
#include <core_api/imagehandler.h>
#include <string>
#include <time.h>
#include <stdio.h>
#include "cl_util.h"

using namespace yafaray;

renderEnvironment_t *env;
imageHandler_t *handler;

unsigned int w = 512;
unsigned int h = 384;
float MinRe = -2.0;
float MaxRe = 1.0;
float MinIm = -1.2;
float MaxIm = MinIm+(MaxRe-MinRe)*h/w;
float Re_factor = (MaxRe-MinRe)/(w-1);
float Im_factor = (MaxIm-MinIm)/(h-1);
unsigned MaxIterations = 60000;

void putN(int x, int y, int n) {
	if(n == MaxIterations) {
		handler->putPixel(x,y, color_t(0,0,0));
	} else {
		if(n < MaxIterations/2) {
			float c = n*255/(float)(MaxIterations/2-1);
			handler->putPixel(x,y, color_t(c, 0,0));
		} else {
			float c = (n-MaxIterations/2)*255/(float)(MaxIterations/2);
			handler->putPixel(x, y, color_t(255, c, c));
		}
	}
}

void doCPU() {
	// from http://warp.povusers.org/Mandelbrot/
	for(unsigned y=0; y<h; ++y)
	{
		double c_im = MaxIm - y*Im_factor;
		for(unsigned x=0; x<w; ++x)
		{
			double c_re = MinRe + x*Re_factor;

			double Z_re = c_re, Z_im = c_im;
			int n;
			for(n=0; n<MaxIterations; ++n)
			{
				double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
				if(Z_re2 + Z_im2 > 4)
					break;
				Z_im = 2*Z_re*Z_im + c_im;
				Z_re = Z_re2 - Z_im2 + c_re;
			}
			putN(x,y,n);
		}
		printf("+");
	}
}

void runOCL( void (*cb)(CLDevice device, CLContext *context, CLCommandQueue *queue) ) {
	CLError err;
	CLMain cl;
	std::list<CLPlatform> platforms = cl.getPlatforms(&err);
	checkErr(err || platforms.empty(), "failed to find platforms\n");
	CLPlatform platform = *platforms.begin();

	std::list<CLDevice> devices = platform.getDevices(&err);
	checkErr(err || devices.empty(), "failed to find devices for the chosen platform");
	CLDevice device = *devices.begin();

	CLContext *context = platform.createContext(device, &err);
	checkErr(err || context == NULL, "failed to get a context for the chosen device\n");

	CLCommandQueue *queue = context->createCommandQueue(device, &err);
	checkErr(err || queue == NULL, "failed to create command queue");

	cb(device, context, queue);
	
	queue->free(&err);
	checkErr(err, "failed to free queue");
	context->free(&err);
	checkErr(err, "failed to free context");
}

const char *kernel_source = CL_SRC(
   __kernel void mandelbrot(
		__global int* a
	)
	{
		typedef struct {
			unsigned int MaxIterations;
			float MaxRe;
			float MinIm;
			float MinRe;
			unsigned int h;
			unsigned int w;
		} const_t;
		const_t ct = {
			60000, 1, -1.2, -2, 384, 512};

		int x = get_global_id(0);
		int y = get_global_id(1);
		if (x >= ct.w || y >= ct.h)
			return;

		float MaxIm = ct.MinIm + (ct.MaxRe - ct.MinRe) * ct.h / ct.w;
		float Re_factor = (ct.MaxRe - ct.MinRe) / (ct.w - 1);
		float Im_factor = (MaxIm - ct.MinIm) / (ct.h - 1);
		

		float c_im = MaxIm - y*Im_factor;
		float c_re = ct.MinRe + x*Re_factor;

		float Z_re = c_re;
		float Z_im = c_im;

		int n;
		for(n = 0; n < ct.MaxIterations; ++n)
		{
			float Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
			if(Z_re2 + Z_im2 > 4)
				break;
			Z_im = 2*Z_re*Z_im + c_im;
			Z_re = Z_re2 - Z_im2 + c_re;
		}

		a[x + y * ct.w] = n;
	}
);

void doOpenCL(CLDevice device, CLContext *context, CLCommandQueue *queue) {
	CLError err;
	int *a = new int[w*h];

	CLBuffer *buf_a = context->createBuffer(CL_MEM_READ_WRITE, w*h*sizeof(int), NULL, &err);
	checkErr(err, "failed to create buffer");

	/*CLSrcConst ct;
	ct["w"] = w;
	ct["h"] = h;
	ct["MinRe"] = MinRe;
	ct["MaxRe"] = MaxRe;
	ct["MinIm"] = MinIm;
	ct["MaxIterations"] = MaxIterations;

	std::string src = (std::string)ct + kernel_source;*/

	CLProgram *program = buildCLProgram(/*src.c_str()*/kernel_source, context, device);

	CLKernel *kernel = program->createKernel("mandelbrot", &err);
	checkErr(err, "failed to create kernel");
	
	kernel->setArgs(buf_a, &err);
	checkErr(err, "failed to set args");
{'fraction_non_alphanumeric': 0.1657901656586905, 'fraction_numerical': 0.01827504601630292, 'mean_word_length': 3.7993690851735016, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2728782', 'n_tokens_mistral': 3424, 'n_tokens_neox': 3112, 'n_words': 742}