uint64_t KVALUE_ToUIntValue(KVALUE* kv) {
	int64_t v64;

	v64 = KVALUE_ToIntValue(kv);

	//
	// returning unsigned integer value depending on type
	//
	switch (kv->kind) {
		case INT1_KIND:
			return v64;
		case INT8_KIND:
			return (uint8_t)v64;
		case INT16_KIND:
			return (uint16_t)v64;
		case INT24_KIND:
		case INT32_KIND:
			return (uint32_t)v64;
		case INT64_KIND:
			return (uint64_t)v64;
		case INT80_KIND:
			DEBUG_STDERR("Unsupported type INT80_KIND.");
			safe_assert(false);
			return v64;
		default:
			return v64;
	}
}
std::string KIND_ToString(int kind) {
	std::stringstream s;
	switch (kind) {
		case PTR_KIND:
			s << "[PTR]";
			break;
		case INT1_KIND:
			s << "[INT1]";
			break;
		case INT8_KIND:
			s << "[INT8]";
			break;
		case INT16_KIND:
			s << "[INT16]";
			break;
		case INT24_KIND:
			s << "[INT24]";
			break;
		case INT32_KIND:
			s << "[INT32]";
			break;
		case INT64_KIND:
			s << "[INT64]";
			break;
		case INT80_KIND:
			s << "[INT80]";
			break;
		case FLP32_KIND:
			s << "[FLP32]";
			break;
		case FLP64_KIND:
			s << "[FLP64]";
			break;
		case FLP80X86_KIND:
			s << "[FLP80X86]";
			break;
		case ARRAY_KIND:
			s << "[ARRAY]";
			break;
		case STRUCT_KIND:
			s << "[STRUCT]";
			break;
		case VOID_KIND:
			s << "[VOID]";
			break;
		default:
			DEBUG_STDERR("Unsupported kind value " << kind);
			safe_assert(false);
			break;
	}
	return s.str();
}
int64_t KVALUE_ToIntValue(KVALUE* kv) {
	int32_t v32;
	int32_t* v32Ptr;
	int64_t v64;
	int64_t* v64Ptr;

	//
	// truncating 64-bit integer value to 32-bit integer value
	//
	v64 = kv->value.as_int;
	v64Ptr = &v64;
	v32Ptr = (int32_t*)v64Ptr;
	v32 = *v32Ptr;

	//
	// returning integer value depending on type
	//
	switch (kv->kind) {
		case INT1_KIND:
			return v32 & 0x1;
			;
		case INT8_KIND:
			return v32 & 0x000000FF;
		case INT16_KIND:
			return v32 & 0x0000FFFF;
		case INT24_KIND:
			return v32 & 0x00FFFFFF;
		case INT32_KIND:
			return v32;
		case INT64_KIND:
			return v64;
		case INT80_KIND:
			DEBUG_STDERR("Unsupported type INT80_KIND.");
			safe_assert(false);
			return v64;
		default:
			return v64;
	}
}
std::string SCOPE_ToString(SCOPE scope) {

	std::stringstream s;

	switch (scope) {
		case GLOBAL:
			s << "GLOBAL";
			break;
		case LOCAL:
			s << "LOCAL";
			break;
		case CONSTANT:
			s << "CONSTANT";
			break;
		case REGISTER:
			s << "REGISTER";
			break;
		default:
			DEBUG_STDERR("Unsupport scope: " << scope);
			safe_assert(false);
	}

	return s.str();
}
long double KVALUE_ToFlpValue(KVALUE* kv) {
	double v64;

	v64 = kv->value.as_flp;

	//
	// returning float value depending on type
	//
	switch (kv->kind) {
		case FLP32_KIND:
			return (float)v64;
		case FLP64_KIND:
			return v64;
		case FLP80X86_KIND:
			return v64;
		case FLP128_KIND:
		case FLP128PPC_KIND:
			DEBUG_STDERR("Unsupported float type: " << kv->kind);
			safe_assert(false);
			return v64;
		default:
			return v64;
	}
}
std::string KVALUE_ToString(KVALUE* kv) {

	std::stringstream s;

	if (kv) {
		s << "[INX: " << kv->inx << " ";

		switch (kv->kind) {
			case PTR_KIND:
				s << "PTR: " << kv->value.as_ptr << "]";
				break;
			case INT1_KIND:
				s << "INT1: " << kv->value.as_int << "]";
				break;
			case INT8_KIND:
				s << "INT8: " << kv->value.as_int << "]";
				break;
			case INT16_KIND:
				s << "INT16: " << kv->value.as_int << "]";
				break;
			case INT24_KIND:
				s << "INT24: " << kv->value.as_int << "]";
				break;
			case INT32_KIND:
				s << "INT32: " << kv->value.as_int << "]";
				break;
			case INT64_KIND:
				s << "INT64: " << kv->value.as_int << "]";
				break;
			case INT80_KIND:
				s << "INT80: " << kv->value.as_int << "]";
				break;
			case FLP32_KIND:
				s << "FLP32: " << kv->value.as_flp << "]";
				break;
			case FLP64_KIND:
				s << "FLP64: " << kv->value.as_flp << "]";
				break;
			case FLP80X86_KIND:
				s << "FLP80X86: " << kv->value.as_flp << "]";
				break;
			case ARRAY_KIND:
				s << "ARRAY: " << kv->value.as_flp << "]";
				break;
			case STRUCT_KIND:
				s << "STRUCT: " << kv->value.as_flp << "]";
				break;
			default:
				safe_assert(false);
				break;
		}

		if (kv->isGlobal) {
			s << " GLOBAL";
		}

		s << "]";
	} else {
		DEBUG_STDERR("KVALUE is NULL.");
		safe_assert(false);
	}
	return s.str();
}
Example #7
0
int Parallel::setup() {
    /**
     * OpenCL initialization.
     */
    cl_int status = Simulator::setup();
    CheckStatus(status, "Simulator::setup() failed.");
    
    cl_uint numPlatforms;
    status = clGetPlatformIDs(0, NULL, &numPlatforms);
    CheckStatus(status, "clGetPlatformIDs, fetching number");
    DEBUG_STDOUT("Number of platforms: " << numPlatforms);
    
    cl_platform_id platform = NULL;
    if (numPlatforms > 0) {
        std::unique_ptr<cl_platform_id[]> platforms (new cl_platform_id[numPlatforms]);
        status = clGetPlatformIDs(numPlatforms, platforms.get(), NULL);
        CheckStatus(status, "clGetPlatformIDs, fetching platforms");
        
        for (unsigned i = 0; i < numPlatforms; ++i) {
            char pbuf[100];
            status = clGetPlatformInfo(platforms[i],
                                       CL_PLATFORM_VENDOR,
                                       sizeof(pbuf),
                                       pbuf,
                                       NULL);
            CheckStatus(status, "clGetPlatformInfo");
        }
        
        // Just grab the first platform.
        platform = platforms[0];
    }
    CheckConditional(platform != NULL, "platform == NULL");
    
    cl_uint numDevices;
    status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
    CheckStatus(status, "clGetDeviceIDs: fetching number");
    DEBUG_STDOUT("Number of devices: " << numDevices);
    
    cl_device_id *devices = new cl_device_id[numDevices];
    status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, numDevices, devices, NULL);
    CheckStatus(status, "clGetDeviceIDs: fetching devices");
    
    int deviceIndex = 0;
    for (unsigned i = 0; i < numDevices; ++i) {
        char pbuf[100];
        status = clGetDeviceInfo(devices[i],
                                 CL_DEVICE_NAME,
                                 sizeof(pbuf),
                                 pbuf,
                                 NULL);
        if (!strncmp(pbuf, "ATI", 3)) {
            deviceIndex = i;
        }
    }
    
    /* Create the context. */
    context = clCreateContext(0, numDevices, devices, NULL, NULL, &status);
    CheckConditional(context != NULL, "clCreateContextFromType");
    
    /* Create command queue */
    cl_command_queue_properties prop = CL_QUEUE_PROFILING_ENABLE;
    commandQueue = clCreateCommandQueue(context, devices[deviceIndex], prop, &status);
    CheckStatus(status, "clCreateCommandQueue");
    
    /* Create a CL program using the kernel source */
    SDKFile kernelFile;
    std::string kernelPath = getenv("HOME") + std::string("/md-simulator/src/TestKernel.cl");
    if(!kernelFile.open(kernelPath.c_str())) {
        DEBUG_STDERR("Failed to load kernel file : " << kernelPath);
        return MD_FAILURE;
    }
    
    const char *source = kernelFile.source().c_str();
    size_t sourceSize[] = {strlen(source)};
    program = clCreateProgramWithSource(context,
                                        1,
                                        &source,
                                        sourceSize,
                                        &status);
    CheckStatus(status, "clCreateProgramWithSource");
    
    /* Create a cl program executable for all the devices specified */
    status = clBuildProgram(program,
                            numDevices,
                            devices,
                            NULL,
                            NULL,
                            NULL);
    
    if (status != CL_SUCCESS) {
        if (status == CL_BUILD_PROGRAM_FAILURE) {
            cl_int logStatus;
            std::unique_ptr<char[]> buildLog (nullptr);
            //char *buildLog = NULL;
            size_t buildLogSize = 0;
            logStatus = clGetProgramBuildInfo(program,
                                              devices[deviceIndex],
                                              CL_PROGRAM_BUILD_LOG,
                                              buildLogSize,
                                              buildLog.get(),
                                              &buildLogSize);
            CheckStatus(logStatus, "clGetProgramBuildInfo");
            
            buildLog = std::unique_ptr<char[]>(new char[buildLogSize]);
            if(!buildLog) {
                return MD_FAILURE;
            }
            std::fill_n(buildLog.get(), buildLogSize, 0);
            
            logStatus = clGetProgramBuildInfo(program,
                                              devices[deviceIndex],
                                              CL_PROGRAM_BUILD_LOG,
                                              buildLogSize,
                                              buildLog.get(),
                                              NULL);
            CheckStatus(logStatus, "clGetProgramBuildInfo (2)");
            
            DEBUG_STDERR("\n\t\t\tBUILD LOG\n");
            DEBUG_STDERR("************************************************\n");
            DEBUG_STDERR(buildLog.get());
            DEBUG_STDERR("************************************************\n");
        }
    }
    CheckStatus(status, "clBuildProgram");
    
    /* Get a kernel object handle for a kernel with the given name */
    kernel = clCreateKernel(program, "computeAccelerations", &status);
    CheckStatus(status, "clCreateKernel");
    
    /* Check group size against group size returned by kernel */
    status = clGetKernelWorkGroupInfo(kernel,
                                      devices[deviceIndex],
                                      CL_KERNEL_WORK_GROUP_SIZE,
                                      sizeof(size_t),
                                      &kernelWorkGroupSize,
                                      0);
    CheckStatus(status, "clGetKernelWorkGroupInfo");
    DEBUG_STDOUT("kernelWorkGroupSize: " << kernelWorkGroupSize);
    
    /**
     * Initialize some simulator data structures.
     */
    global = particleCount * particleCount;
    local = particleCount;
    
    if (global * local > kernelWorkGroupSize) {
        DEBUG_STDERR("WARNING - global * local > kernelWorkGroupSize; global: " << global << ", local: " << local << ", kernelWorkGroupSize: " << kernelWorkGroupSize);
        return MD_FAILURE;
    }
    
    // Data holds the molecule positions.
    data = std::unique_ptr<float[]> (new float[particleCount * 3]);
    
    // Constants holds simulator constants.
    constants = std::unique_ptr<float[]> (new float[NUM_CONSTANTS]);
    
    // Copy constants to buffer;
    constants[0] = epsilon;
    constants[1] = sigma;
    constants[2] = negForceCutoffMinusHalf;
    constants[3] = forceCutoffMinusHalf;
    constants[4] = wallStiffness;
    
    // Results holds pairwise forces.
    results = std::unique_ptr<float[]> (new float[particleCount * particleCount * 3]);
    
    return MD_SUCCESS;
}