/*
 * Copyright 2011-2012 Con Kolivas
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.  See COPYING for more details.
 */

#include "config.h"

#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>

#ifdef WIN32
	#include <winsock2.h>
#else
	#include <sys/socket.h>
	#include <netinet/in.h>
	#include <netdb.h>
#endif

#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <sys/stat.h>
#include <unistd.h>

//#include "findnonce.h"
//#include "algorithm.h"
#include "ocl.h"
//#include "miner.h"


int opt_platform_id = 0;

//void applog(

#define LOG_WARNING 1
#define MAX_GPUDEVICES 2
#define LOG_NOTICE 1
//rror: 'patchbfi' undeclared (first use in this function)
//ocl.c:451:10: error: 'CompilerOptions' undeclared (first use in this function)
//ocl.c:477:2: error: 'prog_built' undeclared (first use in this function)
//ocl.c:634:76: error: 'bufsize' undeclared (first use in this function)
#define BUFFERSIZE 1024



//_clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *algorithm)

_clState *initCl(unsigned int gpu)
{
	_clState *clState = (_clState *)calloc(1, sizeof(_clState));

	struct cgpu_info BLa1;
	//bool patchbfi = false, prog_built = false;



	struct cgpu_info *cgpu = &BLa1;  // = &gpus[gpu];


	cl_platform_id platform = NULL;
	char pbuff[256], vbuff[255];
	cl_platform_id* platforms;
	cl_uint preferred_vwidth;
	cl_device_id *devices;
	cl_uint numPlatforms;
	cl_uint numDevices;
	cl_int status;
	int patchbfi = 0;
	//int CompilerOptions = 0;
	int prog_built = 1;
	int bufsize = 0;

	status = clGetPlatformIDs(0, NULL, &numPlatforms);
	if (status != CL_SUCCESS) {

		printf("Error %d: Getting Platforms. (clGetPlatformsIDs)\n", status);
		return NULL;
	}

	platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
	status = clGetPlatformIDs(numPlatforms, platforms, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Getting Platform Ids. (clGetPlatformsIDs)\n", status);
		return NULL;
	}


	if (opt_platform_id >= (int)numPlatforms) {
		printf("Specified platform that does not exist");
		return NULL;
	}

	status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Getting Platform Info. (clGetPlatformInfo)\n", status);
		return NULL;
	}
	platform = platforms[opt_platform_id];

	if (platform == NULL) {
		perror("NULL platform found!\n");
		return NULL;
	}

	printf("CL Platform vendor: %s\n", pbuff);

	status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
	if (status == CL_SUCCESS)
		printf("CL Platform name: %s\n", pbuff);
	status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
	if (status == CL_SUCCESS)
		printf("CL Platform version: %s\n", vbuff);

	status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
	if (status != CL_SUCCESS) {
		printf("Error %d: Getting Device IDs (num)\n", status);
		return NULL;
	}

	if (numDevices > 0 ) {
		devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));

		/* Now, get the device list data */

		status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
		if (status != CL_SUCCESS) {
			printf("Error %d: Getting Device IDs (list)\n", status);
			return NULL;
		}

		printf("List of devices:\n");

		unsigned int i;
		for (i = 0; i < numDevices; i++) {
			status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
			if (status != CL_SUCCESS) {
				printf("Error %d: Getting Device Info\n", status);
				return NULL;
			}

			printf("\t%i\t%s\n", i, pbuff);
		}

		if (gpu < numDevices) {
			status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
			if (status != CL_SUCCESS) {
				printf("Error %d: Getting Device Info\n", status);
				return NULL;
			}

			printf("Selected %i: %s\n", gpu, pbuff);
			//strncpy(name, pbuff, nameSize);
		} else {
			printf("Invalid GPU %i\n", gpu);
			return NULL;
		}

	} else return NULL;

	cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };

	clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
	if (status != CL_SUCCESS) {
		printf("Error %d: Creating Context. (clCreateContextFromType)\n", status);
		return NULL;
	}

	/////////////////////////////////////////////////////////////////
	// Create an OpenCL command queue
	/////////////////////////////////////////////////////////////////
	clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
						     CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
	if (status != CL_SUCCESS) /* Try again without OOE enable */
		clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
	if (status != CL_SUCCESS) {
		printf("Error %d: Creating Command Queue. (clCreateCommandQueue)\n", status);
		return NULL;
	}

	/* Check for BFI INT support. Hopefully people don't mix devices with
	 * and without it! */
	char * extensions = (char *)malloc(1024);
	const char * camo = "cl_amd_media_ops";
	char *find;

	status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS\n", status);
		return NULL;
	}
	find = strstr(extensions, camo);
	if (find)
		clState->hasBitAlign = true;

	/* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
	char * devoclver = (char *)malloc(1024);
	const char * ocl10 = "OpenCL 1.0";
	const char * ocl11 = "OpenCL 1.1";

	status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION\n", status);
		return NULL;
	}
	find = strstr(devoclver, ocl10);
	if (!find) {
		clState->hasOpenCL11plus = true;
		find = strstr(devoclver, ocl11);
		if (!find)
			clState->hasOpenCL12plus = true;
	}

	status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT\n", status);
		return NULL;
	}
	printf("Preferred vector width reported %d\n", preferred_vwidth);

	status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE\n", status);
		return NULL;
	}
	printf("Max work group size reported %d\n", (int)(clState->max_work_size));

	size_t compute_units = 0;
	status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(size_t), (void *)&compute_units, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_COMPUTE_UNITS\n", status);
		return NULL;
	}
	// AMD architechture got 64 compute shaders per compute unit.
	// Source: http://www.amd.com/us/Documents/GCN_Architecture_whitepaper.pdf
	clState->compute_shaders = compute_units * 64;
	printf("Max shaders calculated %d\n", (int)(clState->compute_shaders));

	status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&cgpu->max_alloc, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE\n", status);
		return NULL;
	}
	printf("Max mem alloc size is %lu\n", (long unsigned int)(cgpu->max_alloc));


 status = clGetDeviceInfo(devices[gpu], CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE, sizeof(cl_ulong), (void *)&cgpu->cacheline_size, NULL);
    if (status != CL_SUCCESS) {
        printf("Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE\n", status);
        return NULL;
    }

    printf("CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE %lu\n", (long unsigned int)(cgpu->cacheline_size));

free(platforms);
return clState;

}


/*	char binaryfilename[255];
	char filename[255];
	char strbuf[32];

	if (cgpu->kernelname == NULL) {
		printf("No kernel specified, defaulting to ckolivas");
		cgpu->kernelname = strdup("ckolivas");
	}

	sprintf(strbuf, "%s.cl", cgpu->kernelname);
	strcpy(filename, strbuf);
	strcpy(binaryfilename, cgpu->kernelname);

	if (strstr(name, "Tahiti"))
		preferred_vwidth = 1;
	else if (preferred_vwidth > 2)
		preferred_vwidth = 2;

	cgpu->vwidth = 1;

	if (cgpu->vwidth)
		clState->vwidth = cgpu->vwidth;
	else {
		clState->vwidth = preferred_vwidth;
		cgpu->vwidth = preferred_vwidth;
	}

	clState->goffset = true;

	if (cgpu->work_size && cgpu->work_size <= clState->max_work_size)
		clState->wsize = cgpu->work_size;
	else
		clState->wsize = 256;

	if (!cgpu->opt_lg) {
		printf("GPU %d: selecting lookup gap of 2", gpu);
		cgpu->lookup_gap = 2;
	} else
		cgpu->lookup_gap = cgpu->opt_lg;

	if ((strcmp(cgpu->kernelname, "zuikkis") == 0) && (cgpu->lookup_gap != 2)) {
		applog(LOG_WARNING, "Kernel zuikkis only supports lookup-gap = 2 (currently %d), forcing.", cgpu->lookup_gap);
		cgpu->lookup_gap = 2;
	}

	if ((strcmp(cgpu->kernelname, "lsoc") == 0) && (cgpu->lookup_gap > 8)) {
		applog(LOG_WARNING, "Kernel lsoc only supports lookup-gap 1 to 8 (currently %d), forcing 8.", cgpu->lookup_gap);
		cgpu->lookup_gap = 8;
	}


	if (!cgpu->opt_tc) {
		unsigned int sixtyfours;

		sixtyfours =  cgpu->max_alloc / 131072 / 64 / (algorithm->n/1024) - 1;
		cgpu->thread_concurrency = sixtyfours * 64;
		if (cgpu->shaders && cgpu->thread_concurrency > cgpu->shaders) {
			cgpu->thread_concurrency -= cgpu->thread_concurrency % cgpu->shaders;
			if (cgpu->thread_concurrency > cgpu->shaders * 5)
				cgpu->thread_concurrency = cgpu->shaders * 5;
		}
		printf("GPU %d: selecting thread concurrency of %d", gpu, (int)(cgpu->thread_concurrency));
	} else
		cgpu->thread_concurrency = cgpu->opt_tc;


	FILE *binaryfile;
	size_t *binary_sizes;
	char **binaries;
	int pl;
	char *source = file_contents(filename, &pl);
	size_t sourceSize[] = {(size_t)pl};
	cl_uint slot, cpnd;

	slot = cpnd = 0;

	if (!source)
		return NULL;

	binary_sizes = (size_t *)calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
	if (!binary_sizes) {
		printf("Unable to calloc binary_sizes");
		return NULL;
	}
	binaries = (char **)calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
	if (!binaries) {
		printf("Unable to calloc binaries");
		return NULL;
	}

	strcat(binaryfilename, name);
	if (clState->goffset)
		strcat(binaryfilename, "g");

//	sprintf(strbuf, "lg%utc%unf%u", cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency, algorithm->nfactor);
	strcat(binaryfilename, strbuf);

	sprintf(strbuf, "w%d", (int)clState->wsize);
	strcat(binaryfilename, strbuf);
	sprintf(strbuf, "l%d", (int)sizeof(long));
	strcat(binaryfilename, strbuf);
	strcat(binaryfilename, ".bin");

	binaryfile = fopen(binaryfilename, "rb");
	if (!binaryfile) {
		printf("No binary found, generating from source");
	} else {
		struct stat binary_stat;

		if (stat(binaryfilename, &binary_stat)) {
			printf("Unable to stat binary, generating from source");
			fclose(binaryfile);
			goto build;
		}
		if (!binary_stat.st_size)
			goto build;

		binary_sizes[slot] = binary_stat.st_size;
		binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
		if (!binaries[slot]) {
			printf("Unable to calloc binaries");
			fclose(binaryfile);
			return NULL;
		}

		if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
			printf("Unable to fread binaries");
			fclose(binaryfile);
			free(binaries[slot]);
			goto build;
		}

		clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, NULL, &status);
		if (status != CL_SUCCESS) {
			printf("Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)\n", status);
			fclose(binaryfile);
			free(binaries[slot]);
			goto build;
		}

		fclose(binaryfile);
		printf("Loaded binary image %s", binaryfilename);

		goto built;
	}

	/////////////////////////////////////////////////////////////////
	// Load CL file, build CL program object, create CL kernel object
	/////////////////////////////////////////////////////////////////

build:
	applog(LOG_NOTICE, "Building binary %s", binaryfilename);

	clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
	if (status != CL_SUCCESS) {
		printf("Error %d: Loading Binary into cl_program (clCreateProgramWithSource)\n", status);
		return NULL;
	}



	char *CompilerOptions = (char *)calloc(1, 256);

	sprintf(CompilerOptions, "-I kernel/ -D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d -D NFACTOR=%d",
			cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency, (int)clState->wsize, (unsigned int)algorithm->nfactor);

	printf("Setting worksize to %d", (int)(clState->wsize));
	if (clState->vwidth > 1)
		printf("Patched source to suit %d vectors", clState->vwidth);

	if (clState->hasBitAlign) {
		strcat(CompilerOptions, " -D BITALIGN");
		printf("cl_amd_media_ops found, setting BITALIGN");
		if (!clState->hasOpenCL12plus &&
		    (strstr(name, "Cedar") ||
		     strstr(name, "Redwood") ||
		     strstr(name, "Juniper") ||
		     strstr(name, "Cypress" ) ||
		     strstr(name, "Hemlock" ) ||
		     strstr(name, "Caicos" ) ||
		     strstr(name, "Turks" ) ||
		     strstr(name, "Barts" ) ||
		     strstr(name, "Cayman" ) ||
		     strstr(name, "Antilles" ) ||
		     strstr(name, "Wrestler" ) ||
		     strstr(name, "Zacate" ) ||
		     strstr(name, "WinterPark" )))
			patchbfi = true;
	} else
		printf("cl_amd_media_ops not found, will not set BITALIGN");


	if (patchbfi) {
		strcat(CompilerOptions, " -D BFI_INT");
		printf("BFI_INT patch requiring device found, patched source with BFI_INT");
	} else
		printf("BFI_INT patch requiring device not found, will not BFI_INT patch");

	if (clState->goffset)
		strcat(CompilerOptions, " -D GOFFSET");

	if (!clState->hasOpenCL11plus)
		strcat(CompilerOptions, " -D OCL1");

	printf("CompilerOptions: %s", CompilerOptions);
	status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
	free(CompilerOptions);

	if (status != CL_SUCCESS) {
		printf("Error %d: Building Program (clBuildProgram)\n", status);
		size_t logSize;
		status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);

		char *log = (char *)malloc(logSize);
		status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
		printf("%s", log);
		return NULL;
	}

	prog_built = true;

#ifdef __APPLE__
	goto built;
#endif

	status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)\n", status);
		return NULL;
	}

	status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
	if (status != CL_SUCCESS) {
		printf("Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)\n", status);
		return NULL;
	}

	for (slot = 0; slot < cpnd; slot++)
		if (binary_sizes[slot])
			break;

	printf("Binary size for gpu %d found in binary slot %d: %d", gpu, slot, (int)(binary_sizes[slot]));
	if (!binary_sizes[slot]) {
		printf("OpenCL compiler generated a zero sized binary, FAIL!");
		return NULL;
	}
	binaries[slot] = (char *)calloc(sizeof(char)* binary_sizes[slot], 1);
	status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
	if (status != CL_SUCCESS) {
		printf("Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)\n", status);
		return NULL;
	}

	if (patchbfi) {
		unsigned remaining = binary_sizes[slot];
		char *w = binaries[slot];
		unsigned int start, length;

		if (!advance(&w, &remaining, ".text"))
			goto build;
		w++; remaining--;
		if (!advance(&w, &remaining, ".text")) {
			w--; remaining++;
		}
		memcpy(&start, w + 285, 4);
		memcpy(&length, w + 289, 4);
		w = binaries[slot]; remaining = binary_sizes[slot];
		if (!advance(&w, &remaining, "ELF"))
			goto build;
		w++; remaining--;
		if (!advance(&w, &remaining, "ELF")) {
			w--; remaining++;
		}
		w--; remaining++;
		w += start; remaining -= start;
		printf("At %p (%u rem. bytes), to begin patching",
			w, remaining);
		patch_opcodes(w, length);

		status = clReleaseProgram(clState->program);
		if (status != CL_SUCCESS) {
			printf("Error %d: Releasing program. (clReleaseProgram)\n", status);
			return NULL;
		}

		clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], NULL, &status);
		if (status != CL_SUCCESS) {
			printf("Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)\n", status);
			return NULL;
		}

		prog_built = false;
	}

	free(source);

	binaryfile = fopen(binaryfilename, "wb");
	if (!binaryfile) {
		printf("Unable to create file %s", binaryfilename);
	} else {
		if (fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
			printf("Unable to fwrite to binaryfile");
			return NULL;
		}
		fclose(binaryfile);
	}
built:
	if (binaries[slot])
		free(binaries[slot]);
	free(binaries);
	free(binary_sizes);


	applog(LOG_NOTICE, "Initialising kernel %s with%s bitalign, %spatched BFI, nfactor %d, n %d",
	       filename, clState->hasBitAlign ? "" : "out", patchbfi ? "" : "un",
	       algorithm->nfactor, algorithm->n);


	if (!prog_built) {
		// create a cl program executable for all the devices specified
		status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
		if (status != CL_SUCCESS) {
			printf("Error %d: Building Program (clBuildProgram)\n", status);
			size_t logSize;
			status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);

			char *log = (char *)malloc(logSize);
			status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
			printf("%s", log);
			return NULL;
		}
	}

	clState->kernel = clCreateKernel(clState->program, "search", &status);
	if (status != CL_SUCCESS) {
		printf("Error %d: Creating Kernel from program. (clCreateKernel)\n", status);
		return NULL;
	}


	size_t ipt = ( / cgpu->lookup_gap +
		      (algorithm->n % cgpu->lookup_gap > 0));
	size_t bufsize = 128 * ipt * cgpu->thread_concurrency;

	 Use the max alloc value which has been rounded to a power of
	 2 greater >= required amount earlier 
	if (bufsize > cgpu->max_alloc) {
		applog(LOG_WARNING, "Maximum buffer memory device %d supports says %lu",
			   gpu, (unsigned long)(cgpu->max_alloc));
		applog(LOG_WARNING, "Your scrypt settings come to %lu", (unsigned long)bufsize);
	}
	printf("Creating scrypt buffer sized %lu", (unsigned long)bufsize);
	clState->padbufsize = bufsize;
	clState->padbuffer8 = NULL;
	clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
	if (status != CL_SUCCESS && !clState->padbuffer8) {
		printf("Error %d: clCreateBuffer (padbuffer8), decrease TC or increase LG", status);
		return NULL;
	}

	clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
	if (status != CL_SUCCESS) {
		printf("Error %d: clCreateBuffer (CLbuffer0)\n", status);
		return NULL;
	}
	clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);

	if (status != CL_SUCCESS) {
		printf("Error %d: clCreateBuffer (outputBuffer)\n", status);
		return NULL;
	}


    clState->gbuff = NULL;
    clState->gbuff = clCreateBuffer(clState->context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, 16*sizeof(cl_uint), (void *)const_masks, &status);
    if (status != CL_SUCCESS && !clState->gbuff) {
        printf("Error %d: clCreateBuffer (lbuff)\n", status);
        return NULL;
    }

    clState->lbuff = NULL;
    clState->lbuff = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 16*sizeof(cl_uint), NULL, &status);
    if (status != CL_SUCCESS && !clState->lbuff) {
        printf("Error %d: clCreateBuffer (lbuff)\n", status);
        return NULL;
    }


	return clState;
}
*/
