<commit_before>#parse("main/Header.vm")
package com.nativelibs4java.opencl;

import com.nativelibs4java.util.EnumValue;
import com.nativelibs4java.util.EnumValues;
import com.nativelibs4java.opencl.library.OpenCLLibrary;
import com.nativelibs4java.util.IOUtils;
import com.nativelibs4java.util.NIOUtils;

import static com.nativelibs4java.opencl.library.OpenCLLibrary.*;
import org.bridj.*;
import static org.bridj.Pointer.*;

import java.io.IOException;
import java.nio.*;
import static com.nativelibs4java.opencl.JavaCL.*;
import static com.nativelibs4java.util.NIOUtils.*;
import java.util.*;
import static com.nativelibs4java.opencl.CLException.*;
import org.bridj.ValuedEnum;
import java.util.logging.Level;
import java.util.logging.Logger;

class ByteOrderHack {
	
	public static ByteOrder guessByteOrderNeededForBuffers(CLDevice device) {
		CLPlatform platform = device.getPlatform();
		PlatformUtils.PlatformKind knownPlatform = PlatformUtils.guessPlatformKind(platform);
		if (knownPlatform != PlatformUtils.PlatformKind.AMDApp)
			return device.getByteOrder();
		else
			return checkByteOrderNeededForBuffers(device);
	}
	public static ByteOrder checkByteOrderNeededForBuffers(CLDevice device) {
		CLContext context = JavaCL.createContext((Map)null, device);
		CLQueue queue = context.createDefaultQueue();
		try {
			int n = 16;
			float testValue = 123456789.101112f;
			final int BIG_INDEX = 0, LITTLE_INDEX = 1;
			
			Pointer<Float> inPtr = Pointer.allocateFloats(n);
			inPtr.order(ByteOrder.BIG_ENDIAN).set(BIG_INDEX, testValue);
			inPtr.order(ByteOrder.LITTLE_ENDIAN).set(LITTLE_INDEX, testValue);
			
			CLBuffer<Float> inOut = context.createFloatBuffer(CLMem.Usage.InputOutput, inPtr);
			CLBuffer<Integer> success = context.createIntBuffer(CLMem.Usage.Output, n);
			
			String src =
				"kernel void compare(global float *inout, global int *success) {\n" +
					"int i = get_global_id(0);\n" +
					"success[i] = inout[i] == " + testValue + ";\n" +
					"inout[i] = " + testValue + ";\n" +
				"}";
				
			CLKernel test = context.createProgram(src).createKernel("compare");
			test.setArgs(inOut, success);
			test.enqueueNDRange(queue, new int[] { n }, new int[] { 1 });
			
			Pointer<Integer> successPtr = success.read(queue);
			Pointer<Float> outPtr = inOut.read(queue);
			
			boolean bigOk = successPtr.get(BIG_INDEX) != 0;
			boolean littleOk = successPtr.get(LITTLE_INDEX) != 0;
			
			if (!(bigOk ^ littleOk))
				throw new RuntimeException("Endianness check failed, kernel recognized both endiannesses...");
			
			{
				int index;
				ByteOrder order;
				if (bigOk) {
					order = ByteOrder.BIG_ENDIAN;
					index = BIG_INDEX;
				} else {
					order = ByteOrder.LITTLE_ENDIAN;
					index = LITTLE_INDEX;
				}
				float value = outPtr.order(order).get(index);
				if (value != testValue)
					throw new RuntimeException("Endianness double-check failed, expected " + testValue + " and found " + value + " instead for endianness " + order);
				
				return order;
			}
		} catch (Throwable ex) {
			throw new RuntimeException("Endianness check failed: " + ex, ex);
		} finally {
			queue.release();
			context.release();
		}
	}
}<commit_msg>JavaCL: added debug info to byte order hack<commit_after>#parse("main/Header.vm")
package com.nativelibs4java.opencl;

import com.nativelibs4java.util.EnumValue;
import com.nativelibs4java.util.EnumValues;
import com.nativelibs4java.opencl.library.OpenCLLibrary;
import com.nativelibs4java.util.IOUtils;
import com.nativelibs4java.util.NIOUtils;

import static com.nativelibs4java.opencl.library.OpenCLLibrary.*;
import org.bridj.*;
import static org.bridj.Pointer.*;

import java.io.IOException;
import java.nio.*;
import static com.nativelibs4java.opencl.JavaCL.*;
import static com.nativelibs4java.util.NIOUtils.*;
import java.util.*;
import static com.nativelibs4java.opencl.CLException.*;
import org.bridj.ValuedEnum;
import java.util.logging.Level;
import java.util.logging.Logger;

class ByteOrderHack {
	
	public static ByteOrder guessByteOrderNeededForBuffers(CLDevice device) {
		CLPlatform platform = device.getPlatform();
		PlatformUtils.PlatformKind knownPlatform = PlatformUtils.guessPlatformKind(platform);
		if (knownPlatform != PlatformUtils.PlatformKind.AMDApp)
			return device.getByteOrder();
		else
			return checkByteOrderNeededForBuffers(device);
	}
	public static ByteOrder checkByteOrderNeededForBuffers(CLDevice device) {
		CLContext context = JavaCL.createContext((Map)null, device);
		CLQueue queue = context.createDefaultQueue();
		try {
			int n = 16;
			float testValue = 123456789.101112f;
			final int BIG_INDEX = 0, LITTLE_INDEX = 1;
			
			Pointer<Float> inPtr = Pointer.allocateFloats(n);
			inPtr.order(ByteOrder.BIG_ENDIAN).set(BIG_INDEX, testValue);
			inPtr.order(ByteOrder.LITTLE_ENDIAN).set(LITTLE_INDEX, testValue);
			
			CLBuffer<Float> inOut = context.createFloatBuffer(CLMem.Usage.InputOutput, inPtr);
			CLBuffer<Integer> success = context.createIntBuffer(CLMem.Usage.Output, n);
			
			String src =
				"kernel void compare(global float *inout, global int *success) {\n" +
					"int i = get_global_id(0);\n" +
					"success[i] = inout[i] == " + testValue + ";\n" +
					"inout[i] = " + testValue + ";\n" +
				"}";
				
			CLKernel test = context.createProgram(src).createKernel("compare");
			test.setArgs(inOut, success);
			test.enqueueNDRange(queue, new int[] { n }, new int[] { 1 });
			
			Pointer<Integer> successPtr = success.read(queue);
			Pointer<Float> outPtr = inOut.read(queue);
			
			boolean bigOk = successPtr.get(BIG_INDEX) != 0;
			boolean littleOk = successPtr.get(LITTLE_INDEX) != 0;
			
			int index, otherIndex;
			ByteOrder order, otherOrder;
			if (bigOk) {
				order = ByteOrder.BIG_ENDIAN;
				index = BIG_INDEX;
				otherOrder = ByteOrder.LITTLE_ENDIAN;
				otherIndex = LITTLE_INDEX;
			} else {
				order = ByteOrder.LITTLE_ENDIAN;
				index = LITTLE_INDEX;
				otherOrder = ByteOrder.BIG_ENDIAN;
				otherIndex = BIG_INDEX;
			}
			float value = outPtr.order(order).get(index);
			float otherValue = outPtr.order(otherOrder).get(otherIndex);
			
			if (JavaCL.debug)
				System.out.println("[" + device + "] Endianness test: bigOk = " + bigOk + ", littleOk = " + littleOk + "; value = " + value + ", otherValue = " + otherValue);
			
			if (!(bigOk ^ littleOk))
				throw new RuntimeException("[" + device + "] Endianness check failed, kernel recognized both endiannesses...");
			
			{
				if (value != testValue || otherValue == testValue)
					throw new RuntimeException("[" + device + "] Endianness double-check failed, expected " + testValue + " and found " + value + " instead for endianness " + order + " (otherValue = " + otherValue + " for " + otherOrder + ")");
				
				return order;
			}
		} catch (Throwable ex) {
			throw new RuntimeException("[" + device + "] Endianness check failed: " + ex, ex);
		} finally {
			queue.release();
			context.release();
		}
	}
}
{'fraction_non_alphanumeric': 0.1251802711277762, 'fraction_numerical': 0.00951831554658206, 'mean_word_length': 4.144658753709199, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '6203184', 'n_tokens_mistral': 2680, 'n_tokens_neox': 2296, 'n_words': 470}