<commit_before>#pragma once
#include "stdafx.h"

#define SafeDelete(x) {if((x) != nullptr) { delete (x); } x = nullptr; }
#define SafeDeleteArray(x) {if((x) != nullptr) { delete[] (x); } x = nullptr; }

namespace MUtil
{
	namespace Gen
	{
		static std::string ReadWholeFile(const std::string& path)
		{
			std::ifstream in(path);
			return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
		}

		static std::wstring StrToWStr(const std::string& str)
		{
			using convert_typeX = std::codecvt_utf8<wchar_t>;
			std::wstring_convert<convert_typeX, wchar_t> converterX;

			return converterX.from_bytes(str);
		}

		static std::string WStrToStr(const std::wstring& wstr)
		{
			using convert_typeX = std::codecvt_utf8<wchar_t>;
			std::wstring_convert<convert_typeX, wchar_t> converterX;

			return converterX.to_bytes(wstr);
		}
	}
	
	namespace CL
	{
		static unsigned int INVALID_OBJECT = 0xFFFFFFFF;
		static const wcahr_t* INVALID_NAME = L"[INVALID]";
		
		static const char* MapErrorToString(cl_int error)
		{
			static const char* errorString[] = {
				"CL_SUCCESS",
				"CL_DEVICE_NOT_FOUND",
				"CL_DEVICE_NOT_AVAILABLE",
				"CL_COMPILER_NOT_AVAILABLE",
				"CL_MEM_OBJECT_ALLOCATION_FAILURE",
				"CL_OUT_OF_RESOURCES",
				"CL_OUT_OF_HOST_MEMORY",
				"CL_PROFILING_INFO_NOT_AVAILABLE",
				"CL_MEM_COPY_OVERLAP",
				"CL_IMAGE_FORMAT_MISMATCH",
				"CL_IMAGE_FORMAT_NOT_SUPPORTED",
				"CL_BUILD_PROGRAM_FAILURE",
				"CL_MAP_FAILURE",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"CL_INVALID_VALUE",
				"CL_INVALID_DEVICE_TYPE",
				"CL_INVALID_PLATFORM",
				"CL_INVALID_DEVICE",
				"CL_INVALID_CONTEXT",
				"CL_INVALID_QUEUE_PROPERTIES",
				"CL_INVALID_COMMAND_QUEUE",
				"CL_INVALID_HOST_PTR",
				"CL_INVALID_MEM_OBJECT - EX: WRONG R/W RIGHTS? WRONG STORAGE CLASS? WRONG ARG INDEX?",
				"CL_INVALID_IMAGE_FORMAT_DESCRIPTOR",
				"CL_INVALID_IMAGE_SIZE",
				"CL_INVALID_SAMPLER",
				"CL_INVALID_BINARY",
				"CL_INVALID_BUILD_OPTIONS",
				"CL_INVALID_PROGRAM",
				"CL_INVALID_PROGRAM_EXECUTABLE",
				"CL_INVALID_KERNEL_NAME",
				"CL_INVALID_KERNEL_DEFINITION",
				"CL_INVALID_KERNEL",
				"CL_INVALID_ARG_INDEX",
				"CL_INVALID_ARG_VALUE",
				"CL_INVALID_ARG_SIZE",
				"CL_INVALID_KERNEL_ARGS",
				"CL_INVALID_WORK_DIMENSION",
				"CL_INVALID_WORK_GROUP_SIZE",
				"CL_INVALID_WORK_ITEM_SIZE",
				"CL_INVALID_GLOBAL_OFFSET",
				"CL_INVALID_EVENT_WAIT_LIST",
				"CL_INVALID_EVENT",
				"CL_INVALID_OPERATION",
				"CL_INVALID_GL_OBJECT",
				"CL_INVALID_BUFFER_SIZE",
				"CL_INVALID_MIP_LEVEL",
				"CL_INVALID_GLOBAL_WORK_SIZE",
			};

			const int errorCount = sizeof(errorString) / sizeof(errorString[0]);

			const int index = -error;

			return (index >= 0 && index < errorCount) ? errorString[index] : "";
		}

		static bool ResultCheck(const cl_int val)
		{
			if (val != CL_SUCCESS)
			{
				std::string s;
				s.append("\OpenCL Error -> ");
				s.append(MapErrorToString(val));
				s.append("\n\n");
				std::wcout << s.c_str();
				return true;
			}
			return false;
		}

		static size_t RoundUpToWGSize(int value, int wgSize)
		{
			int remaining = value % wgSize;
			return value + (remaining == 0 ? 0 : (wgSize - remaining));
		}
		
		class CLImage2D
		{
		private:
			cl_mem mMemObject; // OpenCL image memory object
			std::wstring mName; // Readable name of the image object
			cl_image_format mFormat; // OpenCL image format
			cl_image_desc mDescr; // OpenCL image descriptor
			
		public:
			// Initializes the object with invalid values
			CLImage2D()
				: mMemObject(INVALID_OBJECT),
					mName(INVALID_NAME)
			{
			}
			
			// Creates the OpenCL image object and initializes internal variables. Throws and exception if failed.
			void Initialize(cl_context clContext, const std::wstring& name, size_t w, size_t h, size_t rowPitch, cl_channel_order channelOrder, cl_channel_type channelType)
			{
				mName = name;
				mFormat.image_channel_order = channelOrder;
				mFormat.image_channel_data_type = channelType;
				// Fill descriptor here
				
				// Create object here
				
			}
			
			// Returns true if teh object is alread created and returns false if it is not initialized yet.
			bool IsValid() const
			{
				return mMemObject == INVALID_OBJECT ? false : true;
			}
			
			// Returns the internal cl_mem object
			cl_mem getMemObject() const
			{
				return mMemObject;
			}
			
			// Returns the name of the image object<commit_msg>Update MCL_Utility.hpp<commit_after>#pragma once
#include "stdafx.h"

#define SafeDelete(x) {if((x) != nullptr) { delete (x); } x = nullptr; }
#define SafeDeleteArray(x) {if((x) != nullptr) { delete[] (x); } x = nullptr; }
#define SafeReleaseClMemObject(x) {if((x) != MUtil::CL::INVALID_OBJECT) { clReleaseMemObject(x); x =  MUtil::CL::INVALID_OBJECT; } }

typedef sf::Image SFImage;
typedef sf::Texture SFTexture;

namespace MUtil
{
	namespace Gen
	{
		static std::string ReadWholeFile(const std::string& path)
		{
			std::ifstream in(path);
			return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
		}

		static std::wstring StrToWStr(const std::string& str)
		{
			using convert_typeX = std::codecvt_utf8<wchar_t>;
			std::wstring_convert<convert_typeX, wchar_t> converterX;

			return converterX.from_bytes(str);
		}

		static std::string WStrToStr(const std::wstring& wstr)
		{
			using convert_typeX = std::codecvt_utf8<wchar_t>;
			std::wstring_convert<convert_typeX, wchar_t> converterX;

			return converterX.to_bytes(wstr);
		}
	}
	
	namespace CL
	{
		static unsigned int INVALID_OBJECT = 0xFFFFFFFF;
		static const wcahr_t* INVALID_NAME = L"[INVALID]";
		
		static const char* MapErrorToString(cl_int error)
		{
			static const char* errorString[] = {
				"CL_SUCCESS",
				"CL_DEVICE_NOT_FOUND",
				"CL_DEVICE_NOT_AVAILABLE",
				"CL_COMPILER_NOT_AVAILABLE",
				"CL_MEM_OBJECT_ALLOCATION_FAILURE",
				"CL_OUT_OF_RESOURCES",
				"CL_OUT_OF_HOST_MEMORY",
				"CL_PROFILING_INFO_NOT_AVAILABLE",
				"CL_MEM_COPY_OVERLAP",
				"CL_IMAGE_FORMAT_MISMATCH",
				"CL_IMAGE_FORMAT_NOT_SUPPORTED",
				"CL_BUILD_PROGRAM_FAILURE",
				"CL_MAP_FAILURE",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"CL_INVALID_VALUE",
				"CL_INVALID_DEVICE_TYPE",
				"CL_INVALID_PLATFORM",
				"CL_INVALID_DEVICE",
				"CL_INVALID_CONTEXT",
				"CL_INVALID_QUEUE_PROPERTIES",
				"CL_INVALID_COMMAND_QUEUE",
				"CL_INVALID_HOST_PTR",
				"CL_INVALID_MEM_OBJECT - EX: WRONG R/W RIGHTS? WRONG STORAGE CLASS? WRONG ARG INDEX?",
				"CL_INVALID_IMAGE_FORMAT_DESCRIPTOR",
				"CL_INVALID_IMAGE_SIZE",
				"CL_INVALID_SAMPLER",
				"CL_INVALID_BINARY",
				"CL_INVALID_BUILD_OPTIONS",
				"CL_INVALID_PROGRAM",
				"CL_INVALID_PROGRAM_EXECUTABLE",
				"CL_INVALID_KERNEL_NAME",
				"CL_INVALID_KERNEL_DEFINITION",
				"CL_INVALID_KERNEL",
				"CL_INVALID_ARG_INDEX",
				"CL_INVALID_ARG_VALUE",
				"CL_INVALID_ARG_SIZE",
				"CL_INVALID_KERNEL_ARGS",
				"CL_INVALID_WORK_DIMENSION",
				"CL_INVALID_WORK_GROUP_SIZE",
				"CL_INVALID_WORK_ITEM_SIZE",
				"CL_INVALID_GLOBAL_OFFSET",
				"CL_INVALID_EVENT_WAIT_LIST",
				"CL_INVALID_EVENT",
				"CL_INVALID_OPERATION",
				"CL_INVALID_GL_OBJECT",
				"CL_INVALID_BUFFER_SIZE",
				"CL_INVALID_MIP_LEVEL",
				"CL_INVALID_GLOBAL_WORK_SIZE",
			};

			const int errorCount = sizeof(errorString) / sizeof(errorString[0]);

			const int index = -error;

			return (index >= 0 && index < errorCount) ? errorString[index] : "";
		}

		static bool ResultCheck(const cl_int val)
		{
			if (val != CL_SUCCESS)
			{
				std::string s;
				s.append("\OpenCL Error -> ");
				s.append(MapErrorToString(val));
				s.append("\n\n");
				std::wcout << s.c_str();
				return true;
			}
			return false;
		}

		static size_t RoundUpToWGSize(int value, int wgSize)
		{
			int remaining = value % wgSize;
			return value + (remaining == 0 ? 0 : (wgSize - remaining));
		}
		
		class CLImage2D //RAII, non-copyable
		{
		private:
			cl_mem mMemObject; // OpenCL image memory object
			std::wstring mName; // Readable name of the image object
			cl_image_format mFormat; // OpenCL image format
			cl_image_desc mDescr; // OpenCL image descriptor
			
		public:
         CLImage2D(const CLImage2D&) = delete;
         CLImage2D& operator=(const CLImage2D&) = delete;


         // Destructor
         ~CLImage2D()
         {
           SafeReleaseClMemObject(mMemObject)
         }
			
			// Creates the OpenCL image object and initializes internal variables. Throws and exception if failed.
			void CLImage2D(cl_context clContext, const std::wstring& name, size_t w, size_t h, size_t rowPitch, cl_channel_order channelOrder, cl_channel_type channelType)
			{
				mName = name;
				mFormat.image_channel_order = channelOrder;
				mFormat.image_channel_data_type = channelType;
				// Fill descriptor here
				
				// Create object here
				
			}
			
			// Returns true if the object is alread created and returns false if it is not initialized yet.
			bool IsValid() const
			{
				return mMemObject == INVALID_OBJECT ? false : true;
			}
			
			// Returns the internal cl_mem object
			cl_mem getMemObject() const
			{
				return mMemObject;
			}
			
			// Returns the name of the image object
{'fraction_non_alphanumeric': 0.11616485159086055, 'fraction_numerical': 0.0024556907964979713, 'mean_word_length': 3.547087378640777, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '221166', 'n_tokens_mistral': 4110, 'n_tokens_neox': 3328, 'n_words': 638}