예제 #1
0
func Test_VectAdd(t *testing.T) {
	const elements = 100000

	var kernelSrc = `
__kernel void vectAdd(__global int *A,
                      __global int *B,
                      __global int *C)
{
   int i = get_global_id(0);
   C[i] = A[i] + B[i];
}`

	A := make([]int, elements)
	B := make([]int, elements)
	for i := 0; i < elements; i++ {
		A[i], B[i] = i, i
	}
	bytes := uint32(len(raw.ByteSlice(A)))

	for _, platform := range Platforms {
		for _, dev := range platform.Devices {

			var err error
			var context *Context
			var queue *CommandQueue
			var bufA, bufB, bufC *Buffer
			var program *Program
			var kernel *Kernel

			if context, err = NewContextOfDevices(nil, []Device{dev}); err != nil {
				t.Fatal(err)
			}

			if queue, err = context.NewCommandQueue(dev, QUEUE_NIL); err != nil {
				t.Fatal(err)
			}

			if bufA, err = context.NewBuffer(MEM_READ_ONLY, bytes); err != nil {
				t.Fatal(err)
			}
			if bufB, err = context.NewBuffer(MEM_READ_ONLY, bytes); err != nil {
				t.Fatal(err)
			}
			if bufC, err = context.NewBuffer(MEM_WRITE_ONLY, bytes); err != nil {
				t.Fatal(err)
			}

			if err = queue.EnqueueWriteBuffer(bufA, raw.ByteSlice(A), 0); err != nil {
				t.Fatal(err)
			}
			if err = queue.EnqueueWriteBuffer(bufB, raw.ByteSlice(B), 0); err != nil {
				t.Fatal(err)
			}

			if program, err = context.NewProgramFromSource(kernelSrc); err != nil {
				t.Fatal(err)
			}

			if kernel, err = program.NewKernelNamed("vectAdd"); err != nil {
				t.Fatal(err)
			}

			if err = kernel.SetArg(0, bufA); err != nil {
				t.Fatal(err)
			}
			if err = kernel.SetArg(1, bufB); err != nil {
				t.Fatal(err)
			}
			if err = kernel.SetArg(2, bufC); err != nil {
				t.Fatal(err)
			}

			if err = queue.EnqueueKernel(kernel, 0, elements, 1); err != nil {
				t.Fatal(err)
			}

			if outBuf, err := queue.EnqueueReadBuffer(bufC, 0, bytes); err != nil {
				t.Fatal(err)
			} else {
				C := raw.IntSlice(outBuf)

				for i := 0; i < elements; i++ {
					if C[i] != i<<1 {
						t.Fatal("Output is incorrect")
					}
				}
			}
		}
	}
}
예제 #2
0
func Test_VectAdd(t *testing.T) {
	const elements = 100000

	A := make([]int, elements)
	B := make([]int, elements)
	for i := 0; i < elements; i++ {
		A[i], B[i] = i, i
	}
	bytes := uint32(len(raw.ByteSlice(A)))

	for _, platform := range Platforms {
		for _, dev := range platform.Devices {

			var err error
			var context *Context
			var queue *CommandQueue
			var bufA, bufB, bufC *Buffer
			var program *Program
			var kernel *Kernel

			if context, err = NewContextOfDevices(nil, []Device{dev}); err != nil {
				t.Fatal(err)
			}

			if queue, err = context.NewCommandQueue(dev, QUEUE_NIL); err != nil {
				t.Fatal(err)
			}

			if bufA, err = context.NewBuffer(MEM_READ_ONLY, bytes); err != nil {
				t.Fatal(err)
			}
			if bufB, err = context.NewBuffer(MEM_READ_ONLY, bytes); err != nil {
				t.Fatal(err)
			}
			if bufC, err = context.NewBuffer(MEM_WRITE_ONLY, bytes); err != nil {
				t.Fatal(err)
			}

			if err = queue.EnqueueWriteBuffer(bufA, raw.ByteSlice(A), 0); err != nil {
				t.Fatal(err)
			}
			if err = queue.EnqueueWriteBuffer(bufB, raw.ByteSlice(B), 0); err != nil {
				t.Fatal(err)
			}

			if program, err = context.NewProgramFromFile("vector.cl"); err != nil {
				t.Fatal(err)
			}

			if err = program.Build([]Device{dev}, ""); err != nil {
				t.Fatal(err)
			}

			if kernel, err = program.NewKernelNamed("vectAddInt"); err != nil {
				t.Fatal(err)
			}

			if err = kernel.SetArgs(0, []interface{}{bufA, bufB, bufC}); err != nil {
				t.Fatal(err)
			}

			if err = queue.EnqueueKernel(kernel, []Size{0}, []Size{elements}, []Size{1}); err != nil {
				t.Fatal(err)
			}

			if outBuf, err := queue.EnqueueReadBuffer(bufC, 0, bytes); err != nil {
				t.Fatal(err)
			} else {
				C := raw.IntSlice(outBuf)

				for i := 0; i < elements; i++ {
					if C[i] != i<<1 {
						t.Fatal("Output is incorrect")
					}
				}
			}
		}
	}
}