예제 #1
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")
					}
				}
			}
		}
	}
}
예제 #2
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")
					}
				}
			}
		}
	}
}
예제 #3
0
func main() {
	var err error

	if *help {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		return
	}

	var input io.Reader
	if len(*inFilename) == 0 {
		input = os.Stdin
	} else {
		if input, err = os.Open(*inFilename); err != nil {
			fatalError(err)
		}
	}

	var output io.Writer
	if len(*outFilename) == 0 {
		output = os.Stdout
	} else {
		if output, err = os.Create(*outFilename); err != nil {
			fatalError(err)
		}
	}

	var inImage image.Image
	var inFormat string
	if inImage, inFormat, err = image.Decode(input); err != nil {
		fatalError(err)
	}

	size := inImage.Bounds().Size()
	var pixels []uint32
	if pixels, err = imagePixels(inImage); err != nil {
		fatalError(err)
	}
	_ = pixels

	for _, platform := range cl.Platforms {
		for _, dev := range platform.Devices {
			var context *cl.Context
			var queue *cl.CommandQueue
			var sourceImage, destImage *cl.Image
			var sampler *cl.Sampler
			var program *cl.Program
			var kernel *cl.Kernel
			var outPixels []byte

			if context, err = cl.NewContextOfDevices(map[cl.ContextParameter]interface{}{cl.CONTEXT_PLATFORM: platform}, []cl.Device{dev}); err != nil {
				fatalError(err)
			}

			format := cl.ImageFormat{ChannelOrder: cl.RGBA, ChannelDataType: cl.UNSIGNED_INT32}
			if sourceImage, err = context.NewImage2D(cl.MEM_READ_ONLY, format, uint32(size.X), uint32(size.Y), 0, nil); err != nil {
				fatalError(err)
			}

			if destImage, err = context.NewImage2D(cl.MEM_WRITE_ONLY, format, uint32(size.X), uint32(size.Y), 0, nil); err != nil {
				fatalError(err)
			}

			if sampler, err = context.NewSampler(false, cl.ADDRESS_CLAMP, cl.FILTER_NEAREST); err != nil {
				fatalError(err)
			}

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

			if program, err = context.NewProgramFromFile("rotate.cl"); err != nil {
				fatalError(err)
			}

			if err = program.Build(nil, ""); err != nil {
				if status := program.BuildStatus(dev); status != cl.BUILD_SUCCESS {
					fatalError(&customError{fmt.Sprintf("Build Error:\n%s\n", program.Property(dev, cl.BUILD_LOG))})
				}
				fatalError(err)
			}

			if kernel, err = program.NewKernelNamed("imageRotate"); err != nil {
				fatalError(err)
			}

			if err = queue.EnqueueWriteImage(sourceImage, true, [3]cl.Size{0, 0, 0}, [3]cl.Size{cl.Size(size.X), cl.Size(size.Y), 1}, 0, 0, raw.ByteSlice(pixels)); err != nil {
				fatalError(err)
			}

			if err = kernel.SetArgs(0, []interface{}{
				sourceImage, destImage,
				float32(math.Sin(*angle * math.Pi / 180)),
				float32(math.Cos(*angle * math.Pi / 180)),
				sampler}); err != nil {
				fatalError(err)
			}

			if err = queue.EnqueueKernel(kernel, []cl.Size{0, 0, 0}, []cl.Size{cl.Size(size.X), cl.Size(size.Y), 1}, []cl.Size{1, 1, 1}); err != nil {
				fatalError(err)
			}

			if outPixels, err = queue.EnqueueReadImage(destImage, true, [3]cl.Size{0, 0, 0}, [3]cl.Size{cl.Size(size.X), cl.Size(size.Y), 1}, 0, 0); err != nil {
				fatalError(err)
			}

			outImage := image.NewNRGBA(inImage.Bounds())
			for i := 0; i < len(outImage.Pix); i++ {
				outImage.Pix[i] = uint8(outPixels[4*i])
			}

			switch inFormat {
			case "jpeg":
				if err = jpeg.Encode(output, outImage, nil); err != nil {
					fatalError(err)
				}
			case "png":
				if err = png.Encode(output, outImage); err != nil {
					fatalError(err)
				}
			default:
				fatalError(&customError{fmt.Sprintf("Unknown Format: %s", inFormat)})
			}
			return
		}
	}

}