Ejemplo n.º 1
0
func TestContext(t *testing.T) {
	/* Host/device data structures */
	var platforms []ocl.Platform
	var devices []ocl.Device
	var context ocl.Context
	var err error

	var ref_count interface{}
	user_data := []byte("Hello, I am callback")

	t2 = t

	/* Identify a platform */
	if platforms, err = ocl.GetPlatforms(); err != nil {
		t.Errorf(err.Error())
		return
	}

	/* Determine connected devices */
	if devices, err = platforms[0].GetDevices(cl.CL_DEVICE_TYPE_GPU); err != nil {
		if devices, err = platforms[0].GetDevices(cl.CL_DEVICE_TYPE_CPU); err != nil {
			t.Errorf(err.Error())
			return
		}
	}

	/* Create the context */
	if context, err = devices[0].CreateContext(nil, my_contex_notify, unsafe.Pointer(&user_data)); err != nil {
		t.Errorf(err.Error())
		return
	}
	defer context.Release()

	/* Get the reference count */
	if ref_count, err = context.GetInfo(cl.CL_CONTEXT_REFERENCE_COUNT); err != nil {
		t.Errorf(err.Error())
		return
	}
	t.Logf("Initial reference count: %d\n", ref_count.(cl.CL_uint))

	/* Update and display the reference count */
	context.Retain()
	if ref_count, err = context.GetInfo(cl.CL_CONTEXT_REFERENCE_COUNT); err != nil {
		t.Errorf(err.Error())
		return
	}
	t.Logf("Reference count: %d\n", ref_count.(cl.CL_uint))

	context.Release()
	if ref_count, err = context.GetInfo(cl.CL_CONTEXT_REFERENCE_COUNT); err != nil {
		t.Errorf(err.Error())
		return
	}
	t.Logf("Reference count: %d\n", ref_count.(cl.CL_uint))
}
Ejemplo n.º 2
0
func TestPlatform(t *testing.T) {
	/* Host data structures */
	var platforms []ocl.Platform
	var err error

	/* Param value */
	var param_value interface{}
	const icd_ext string = "cl_khr_icd"

	/* Get all installed platforms */
	if platforms, err = ocl.GetPlatforms(); err != nil {
		t.Errorf(err.Error())
		return
	} else {
		t.Logf("Number of platform: %d\n", len(platforms))
	}

	/* Find extensions of all platforms */
	platform_index := -1
	for i := 0; i < len(platforms); i++ {
		DisplayPlatformInfo(t, platforms[i], cl.CL_PLATFORM_PROFILE, "CL_PLATFORM_PROFILE")
		DisplayPlatformInfo(t, platforms[i], cl.CL_PLATFORM_VERSION, "CL_PLATFORM_VERSION")
		DisplayPlatformInfo(t, platforms[i], cl.CL_PLATFORM_NAME, "CL_PLATFORM_NAME")
		DisplayPlatformInfo(t, platforms[i], cl.CL_PLATFORM_VENDOR, "CL_PLATFORM_VENDOR")
		DisplayPlatformInfo(t, platforms[i], cl.CL_PLATFORM_EXTENSIONS, "CL_PLATFORM_EXTENSIONS")

		/* Get extension data */
		if param_value, err = platforms[i].GetInfo(cl.CL_PLATFORM_EXTENSIONS); err != nil {
			t.Errorf(err.Error())
			return
		} else {
			/* Look for ICD extension */
			if strings.Contains(param_value.(string), icd_ext) {
				platform_index = i
				break
			}
		}
	}

	/* Display whether ICD extension is supported */
	if platform_index > -1 {
		t.Logf("platform %d supports the %s extension.\n", platform_index, icd_ext)
	} else {
		t.Logf("No platforms support the %s extension.\n", icd_ext)
	}
}
Ejemplo n.º 3
0
func TestDevice(t *testing.T) {
	/* Host/device data structures */
	var platforms []ocl.Platform
	var devices []ocl.Device
	var err error

	/* Param data */
	var param_value interface{}

	/* Identify a platform */
	if platforms, err = ocl.GetPlatforms(); err != nil {
		t.Errorf(err.Error())
		return
	}

	/* Determine connected devices */
	if devices, err = platforms[0].GetDevices(cl.CL_DEVICE_TYPE_ALL); err != nil {
		t.Errorf(err.Error())
		return
	} else {
		t.Logf("Number of device: %d\n", len(devices))
	}

	/* Obtain data for each connected device */
	for i := 0; i < len(devices); i++ {
		if param_value, err = devices[i].GetInfo(cl.CL_DEVICE_NAME); err != nil {
			t.Errorf("Failed to find OpenCL device info %s.\n", "CL_DEVICE_NAME")
			return
		} else {
			t.Logf("\t%s:\t %v\n", "CL_DEVICE_NAME", param_value)
		}

		if param_value, err = devices[i].GetInfo(cl.CL_DEVICE_TYPE); err != nil {
			t.Errorf("Failed to find OpenCL device info %s.\n", "CL_DEVICE_TYPE")
			return
		} else {
			switch param_value.(cl.CL_device_type) {
			case cl.CL_DEVICE_TYPE_CPU:
				t.Logf("\t%s:\t %v\n", "CL_DEVICE_TYPE", "CL_DEVICE_TYPE_CPU")
			case cl.CL_DEVICE_TYPE_GPU:
				t.Logf("\t%s:\t %v\n", "CL_DEVICE_TYPE", "CL_DEVICE_TYPE_GPU")
			}

		}
	}
}
Ejemplo n.º 4
0
func TestQueue(t *testing.T) {
	/* Host/device data structures */
	var platforms []ocl.Platform
	var devices []ocl.Device
	var context ocl.Context
	var queue ocl.CommandQueue
	var err error

	var ref_count interface{}

	/* Identify a platform */
	if platforms, err = ocl.GetPlatforms(); err != nil {
		t.Errorf(err.Error())
		return
	}

	/* Determine connected devices */
	if devices, err = platforms[0].GetDevices(cl.CL_DEVICE_TYPE_GPU); err != nil {
		if devices, err = platforms[0].GetDevices(cl.CL_DEVICE_TYPE_CPU); err != nil {
			t.Errorf(err.Error())
			return
		}
	}
	devices = devices[0:1]

	/* Create the context */
	if context, err = devices[0].CreateContext(nil, nil, nil); err != nil {
		t.Errorf(err.Error())
		return
	}
	defer context.Release()

	/* Create the command queue */
	if queue, err = context.CreateCommandQueue(devices[0], nil); err != nil {
		t.Errorf(err.Error())
		return
	}
	defer queue.Release()

	/* Get the reference count */
	if ref_count, err = queue.GetInfo(cl.CL_QUEUE_REFERENCE_COUNT); err != nil {
		t.Errorf(err.Error())
		return
	}
	t.Logf("Initial reference count: %d\n", ref_count.(cl.CL_uint))

	/* Update and display the reference count */
	queue.Retain()
	if ref_count, err = queue.GetInfo(cl.CL_QUEUE_REFERENCE_COUNT); err != nil {
		t.Errorf(err.Error())
		return
	}
	t.Logf("Reference count: %d\n", ref_count.(cl.CL_uint))

	queue.Release()
	if ref_count, err = queue.GetInfo(cl.CL_QUEUE_REFERENCE_COUNT); err != nil {
		t.Errorf(err.Error())
		return
	}
	t.Logf("Reference count: %d\n", ref_count.(cl.CL_uint))

	/* Program/kernel data structures */
	var program ocl.Program
	var program_size [1]cl.CL_size_t
	var program_buffer [1][]byte
	var program_log interface{}

	/* Read each program file and place content into buffer array */
	program_handle, err1 := os.Open("blank.cl")
	if err1 != nil {
		t.Errorf(err1.Error())
		return
	}
	defer program_handle.Close()

	fi, err2 := program_handle.Stat()
	if err2 != nil {
		t.Errorf(err2.Error())
		return
	}
	program_size[0] = cl.CL_size_t(fi.Size())
	program_buffer[0] = make([]byte, program_size[0])
	read_size, err3 := program_handle.Read(program_buffer[0])
	if err3 != nil || cl.CL_size_t(read_size) != program_size[0] {
		t.Errorf("read file error or file size wrong")
		return
	}

	// Create program from file
	if program, err = context.CreateProgramWithSource(1, program_buffer[:], program_size[:]); err != nil {
		t.Errorf(err.Error())
		return
	}
	defer program.Release()

	/* Build program */
	if err = program.Build(devices, nil, nil, nil); err != nil {
		t.Errorf(err.Error())
		/* Find size of log and print to std output */
		if program_log, err = program.GetBuildInfo(devices[0], cl.CL_PROGRAM_BUILD_LOG); err != nil {
			t.Errorf(err.Error())
		} else {
			t.Errorf("%s\n", program_log.(string))
		}
		return
	}

	//var kernel cl.CL_kernel
	// /* Create the kernel */
	// kernel = cl.CLCreateKernel(program, []byte("blank"), &err)
	// if err < 0 {
	// 	t.Errorf("Couldn't create the kernel")
	// }

	// /* Enqueue the kernel execution command */
	// err = cl.CLEnqueueTask(queue, kernel, 0, nil, nil)
	// if err < 0 {
	// 	t.Errorf("Couldn't enqueue the kernel execution command")
	// } else {
	// 	t.Logf("Successfully queued kernel.\n")
	// }

	//cl.CLReleaseKernel(kernel)

}