Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis12-cl/src/compiler/cl-target/cl-ty-translate.sml
ViewVC logotype

View of /branches/vis12-cl/src/compiler/cl-target/cl-ty-translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3159 - (download) (annotate)
Sat Mar 28 09:40:50 2015 UTC (3 years, 11 months ago) by jhr
File size: 4572 byte(s)
  bug fixes for images in OpenCL target
(* cl-ty-translate.sml
 *
 * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * For the OpenCL target, we have four representations of Diderot types:
 *
 *	1) the external C representation (see c-util/c-ty-translate.sml)
 *
 *	2) the CPU representation of Diderot values used to compute global
 *	   values (see c-util/c-ty-translate.sml)
 *
 *	3) the CPU-side representation of the GPU types (supported by this file)
 *
 *	4) the GPU-side representation (supported by this file)
 *
 * Copying data between representation #1 and #2 are supported by c-util/c-ty-translate.sml,
 * while this file supports copying data between representations #2 and #3.
 *)

structure CLTyTranslate : sig

  (* translate a type to its the CPU-side shadow type *)
    val toCPUType : TreeIL.Ty.ty -> CLang.ty

  (* translate a type to its the GPU-side OpenCL type *)
    val toGPUType : TreeIL.Ty.ty -> CLang.ty

  (* GPU type for image data pointers *)
    val imageDataPtrTy : ImageInfo.info -> CLang.ty

  (* translate a type to its GPU-side output type *)
    val toOutputType : TreeIL.Ty.ty -> (CLang.ty * int)

  (* generate code to copy values from their internal Diderot representation to their
   * output buffer representation.  We assume that the dst expression has type T_Ptr(ty).
   *)
    val copyToOutput : {ty : TreeIL.Ty.ty, dst : CLang.exp, src : CLang.exp} -> CLang.stm list

  end = struct

    structure Ty = TreeIL.Ty
    structure CL = CLang
    structure CN = CNames
    structure ON = OCLNames

  (* translate a type to its CPU-side shadow type *)
    fun toCPUType ty = (case ty
	   of Ty.BoolTy => CL.T_Named "cl_bool"
	    | Ty.StringTy => raise Fail "CLTyTranslate.toCPUType: StringTy"
	    | Ty.IntTy => !CN.gCPUIntTy
	    | Ty.TensorTy[] => !CN.gCPURealTy
	    | Ty.TensorTy dd => CL.T_Array(!CN.gCPURealTy, SOME(List.foldl Int.* 1 dd))
	    | Ty.SeqTy(Ty.IntTy, n) => CL.T_Array(!CN.gCPUIntTy, SOME n)
	    | Ty.SeqTy(ty, n) => CL.T_Array(toCPUType ty, SOME n)
(* what do we do with the following types?
	    | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy)
	    | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info))
*)
(* FIXME: the following is probably not right, but we have to figure this out *)
	    | Ty.ImageTy info => CN.imagePtrTy(ImageInfo.dim info)
	    | _ => raise Fail(concat["CLTyTranslate.toCPUType(", Ty.toString ty, ")"])
	  (* end case *))

    fun imageDataPtrTy info =
	  CL.T_Qual("__global", CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info)))

  (* translate a type to its GPU-side OpenCL type *)
    fun toGPUType ty = (case ty
	   of Ty.BoolTy => CL.T_Named "bool"
	    | Ty.StringTy => raise Fail "CLTyTranslate.toGPUType: StringTy"
	    | Ty.IntTy => !ON.gIntTy
	    | Ty.TensorTy[] => !ON.gRealTy
            | Ty.TensorTy[n] => ON.vecTy n
            | Ty.TensorTy[n, m] => ON.matTy(n,m)
	    | Ty.TensorTy dd => CL.T_Array(!ON.gRealTy, SOME(List.foldl Int.* 1 dd))
            | Ty.SeqTy(Ty.IntTy, n) => ON.ivecTy n
            | Ty.SeqTy(Ty.TensorTy[] , n) => ON.vecTy n
            | Ty.SeqTy(ty, n) => CL.T_Array(toGPUType ty, SOME n)
            | Ty.AddrTy info => imageDataPtrTy info
            | Ty.ImageTy info => CL.T_Named(CN.imageTyName(ImageInfo.dim info))
	    | _ => raise Fail(concat["CLTyTranslate.toCPUType(", Ty.toString ty, ")"])
	  (* end case *))

  (* translate a type to its GPU-side output type *)
    fun toOutputType ty = (case ty
	   of Ty.BoolTy => (CL.T_Named "uint8", 1)
	    | Ty.StringTy => raise Fail "CLTyTranslate.toGPUType: StringTy"
	    | Ty.IntTy => (!ON.gIntTy, 1)
	    | Ty.TensorTy shape => (!ON.gRealTy, List.foldl Int.* 1 shape)
	    | _ => raise Fail(concat["CLTyTranslate.toOutputType(", Ty.toString ty, ")"])
	  (* end case *))

    fun subscript (e, i) = CL.mkSubscript(e, CL.mkInt(IntInf.fromInt i))

  (* generate code to copy values from their internal Diderot representation to their
   * output buffer representation.
   *)
    fun copyToOutput {ty, dst, src} = (case ty
           of Ty.BoolTy => [CL.mkAssign(CL.mkUnOp(CL.%*, dst), src)]
            | Ty.StringTy => raise Fail "OpenCL does not support strings"
            | Ty.IntTy => [CL.mkAssign(CL.mkUnOp(CL.%*, dst), src)]
            | Ty.TensorTy[] => [CL.mkAssign(CL.mkUnOp(CL.%*, dst), src)]
            | Ty.TensorTy[n] => 
		List.tabulate (n,
		  fn i => CL.mkAssign(subscript(dst, i), CL.mkSelect(src, "s" ^ Int.toString i)))
            | _ => raise Fail(concat["CLTyTranslate.copyToOutput(", Ty.toString ty, ")"])
	  (* end case *))

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0