-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDslAPI.scala
1195 lines (1095 loc) · 50.7 KB
/
DslAPI.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package lantern
import org.scala_lang.virtualized.virtualize
import org.scala_lang.virtualized.SourceContext
import scala.virtualization.lms.common._
import java.nio._
import java.nio.file._
import java.io._
trait DslOps extends PrimitiveOps with NumericOpsExtra with BooleanOps
with LiftString with LiftPrimitives with LiftNumeric with LiftBoolean
with IfThenElse with Equal with RangeOps with OrderingOps with MiscOps with ArrayOps with StringOps
with Functions with While with StaticData
with Variables with LiftVariables with UtilOps with UncheckedOps
with MathOps with TupleOps with TupledFunctions with CastingOps with ScannerOps {
// implicit def repStrToSeqOps(a: Rep[String]) = new SeqOpsCls(a.asInstanceOf[Rep[Seq[Char]]])
implicit class BooleanOps2(lhs: Rep[Boolean]) {
def &&(rhs: =>Rep[Boolean])(implicit pos: SourceContext) = __ifThenElse(lhs, rhs, unit(false))
}
// override def boolean_and(lhs: Rep[Boolean], rhs: Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean] = __ifThenElse(lhs, rhs, unit(false))
// Raw code/comment operations.
def generateRawCode(s: String): Rep[Unit]
def generateRawComment(s: String): Rep[Unit]
def comment[A:Manifest](s: String, verbose: Boolean = true)(b: => Rep[A]): Rep[A]
// added by Fei
def mutableStaticData[T:Manifest](x: T): Rep[T]
}
trait DslExp extends DslOps
with PrimitiveOpsExpOpt with NumericOpsExpOpt with NumericOpsExtraExp with BooleanOpsExpOpt
with IfThenElseExpOpt with EqualExpBridgeOpt with RangeOpsExp with OrderingOpsExpOpt
with MiscOpsExp with EffectExp with ArrayOpsExpOpt with StringOpsExp
with FunctionsRecursiveExp with WhileExp with StaticDataExp
with UtilOpsExp with UncheckedOpsExp with MathOpsExp
with TupleOps with TupledFunctionsExp with CastingOpsExp
with ScannerOpsExp {
override def boolean_or(lhs: Exp[Boolean], rhs: Exp[Boolean])(implicit pos: SourceContext) : Exp[Boolean] = lhs match {
case Const(false) => rhs
case _ => super.boolean_or(lhs, rhs)
}
override def boolean_and(lhs: Exp[Boolean], rhs: Exp[Boolean])(implicit pos: SourceContext) : Exp[Boolean] = lhs match {
case Const(true) => rhs
case _ => super.boolean_and(lhs, rhs)
}
// A raw snippet of code, to be code generated literally.
case class RawCode(s: String) extends Def[Unit]
def generateRawCode(s: String) = reflectEffect(RawCode(s))
case class RawComment(s: String) extends Def[Unit]
def generateRawComment(s: String) = reflectEffect(RawComment(s))
// TODO: Add Shift Node () just like the RawComment node above
// TODO: not here: add a TypeAnalysis trait
// trait TypeAnalysis extends GenericNestedGodegen {
// override def emitNode(sys: Sym[Any], rhs: Def[Any]) = rhs match {
// case Shift(??,??) => {
// ???
// }
// }
// }
case class Comment[A:Manifest](s: String, verbose: Boolean, b: Block[A]) extends Def[A]
def comment[A:Manifest](s: String, verbose: Boolean)(b: => Rep[A]): Rep[A] = {
val br = reifyEffects(b)
val be = summarizeEffects(br)
super.reflectEffect[A](Comment(s, verbose, br), be)
}
override def boundSyms(e: Any): List[Sym[Any]] = e match {
case Comment(_, _, b) => effectSyms(b)
case _ => super.boundSyms(e)
}
override def array_apply[T:Manifest](x: Exp[Array[T]], n: Exp[Int])(implicit pos: SourceContext): Exp[T] = (x,n) match {
case (Def(StaticData(x:Array[T])), Const(n)) =>
val y = x(n)
if (y.isInstanceOf[Int]) unit(y) else staticData(y)
// case _ => super.array_apply(x,n)
// FIXME!!!
case _ => reflectEffect(ArrayApply(x, n))
}
// override def array_apply[T:Manifest](x: Exp[Array[T]], n: Exp[Int])(implicit pos: SourceContext): Exp[T] = reflectEffect(ArrayApply(x, n))
override def array_update[T:Manifest](x: Exp[Array[T]], n: Exp[Int], y: Exp[T])(implicit pos: SourceContext) = reflectEffect(ArrayUpdate(x,n,y))
/*
override def array_update[T:Manifest](x: Exp[Array[T]], n: Exp[Int], y: Exp[T])(implicit pos: SourceContext) = {
if (context ne null) {
// find the last modification of array x
// if it is an assigment at index n with the same value, just do nothing
val vs = x.asInstanceOf[Sym[Array[T]]]
//TODO: could use calculateDependencies?
val rhs = context.reverse.collectFirst {
//case w @ Def(Reflect(ArrayNew(sz: Exp[T]), _, _)) if w == x => Some(Const(())) // FIXME: bounds check!
case Def(Reflect(ArrayUpdate(`x`, `n`, `y`), _, _)) => Some(Const(()))
case Def(Reflect(_, u, _)) if mayWrite(u, List(vs)) => None // not a simple assignment
}
rhs.flatten.getOrElse(super.array_update(x,n,y))
} else {
reflectEffect(ArrayUpdate(x,n,y))
}
}
*/
// TODO: should this be in LMS?
override def isPrimitiveType[T](m: Manifest[T]) = (m == manifest[String]) || super.isPrimitiveType(m)
// should probably add to LMS
def mutableStaticData[T:Manifest](x: T): Exp[T] = reflectMutable(StaticData(x))
override def doApply[A:Manifest,B:Manifest](f: Exp[A => B], x: Exp[A])(implicit pos: SourceContext): Exp[B] = {
val x1 = unbox(x)
val x1_effects = x1 match {
case UnboxedTuple(l) => l.foldLeft(Pure())((b,a)=>a match {
case Def(Lambda(_, _, yy)) => b orElse summarizeEffects(yy)
case _ => b
})
case _ => Pure()
}
f match {
case Def(Lambda(_, _, y)) => reflectEffect(Apply(f, x1), summarizeEffects(y) andAlso x1_effects)
case _ => reflectEffect(Apply(f, x1), Simple() andAlso x1_effects)
}
}
}
trait DslGPUExp extends DslExp with GPUOpsExp
trait DslGenScala extends ScalaGenNumericOps
with ScalaGenPrimitiveOps with ScalaGenBooleanOps with ScalaGenIfThenElse
with ScalaGenEqual with ScalaGenRangeOps with ScalaGenOrderingOps
with ScalaGenMiscOps with ScalaGenArrayOps with ScalaGenStringOps
with ScalaGenFunctions with ScalaGenWhile
with ScalaGenStaticData with ScalaGenVariables
with ScalaGenUtilOps with ScalaGenMathOps with ScalaGenTupledFunctions
with ScalaGenCastingOps {
val IR: DslExp
import IR._
override def quote(x: Exp[Any]) = x match {
case Const('\n') if x.tp == manifest[Char] => "'\\n'"
case Const('\t') if x.tp == manifest[Char] => "'\\t'"
case Const(0) if x.tp == manifest[Char] => "'\\0'"
case _ => super.quote(x)
}
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case afs@ArrayFromSeq(xs) =>
stream.println(remap(afs.m) + " " + quote(sym) + "[" + xs.length + "] = {" + (xs mkString ",") + "}")
case Assign(Variable(a), b) =>
emitAssignment(a.asInstanceOf[Sym[Variable[Any]]], quote(b))
case IfThenElse(c,Block(Const(true)),Block(Const(false))) =>
emitValDef(sym, quote(c))
case PrintF(f:String,xs) =>
emitValDef(sym, src"printf(${Const(f)::xs})")
case RawCode(s) =>
stream.println(s)
case RawComment(s) =>
stream.println("// "+s)
case Comment(s, verbose, b) =>
stream.println("val " + quote(sym) + " = {")
stream.println("//#" + s)
if (verbose) {
stream.println("// generated code for " + s.replace('_', ' '))
} else {
stream.println("// generated code")
}
emitBlock(b)
stream.println(quote(getBlockResult(b)))
stream.println("//#" + s)
stream.println("}")
// case FieldApply() => super.emitNode(sym, rhs)
// case FieldApply(a, "_1") => emitValDef(sym, quote(a) + "._1")
// case FieldApply(a, "_2") => emitValDef(sym, quote(a) + "._2")
case _ => super.emitNode(sym, rhs)
}
override def getFreeDataExp[A](sym: Sym[A], rhs: Def[A]): List[(Sym[Any],Any)] = rhs match {
case Reflect(StaticData(x), _, _) => List((sym,x))
case _ => super.getFreeDataExp(sym, rhs)
}
}
// TODO: currently part of this is specific to the query tests. generalize? move?
trait DslGenBase extends CGenNumericOpsExtra
with CGenPrimitiveOps with CGenBooleanOps with CGenIfThenElse
with CGenEqual with CGenRangeOps with CGenOrderingOps
with CGenMiscOps with CGenArrayOps with CGenStringOps
with CGenFunctions with CGenWhile
with CGenStaticData with CGenVariables
with CGenUtilOps with CGenUncheckedOps with CGenMathOps with CGenTupledFunctions
with CGenCastingOps {
val IR: DslExp
import IR._
def getMallocString(count: String, dataType: String): String = {
"(" + dataType + "*)malloc(" + count + " * sizeof(" + dataType + "));"
}
def getMallocArenaString(count: String, dataType: String): String = {
"(" + dataType + "*)myMalloc(" + count + " * sizeof(" + dataType + "));"
}
// In LMS code, it was "remap(m) + addRef(m)" which would put an extra "*"
override def remapWithRef[A](m: Manifest[A]): String = remap(m) + " "
def unwrapTupleStr(s: String): Array[String] = {
if (s.startsWith("scala.Tuple")) s.slice(s.indexOf("[")+1,s.length-1).filter(c => c != ' ').split(",")
else scala.Array(s)
}
override def remap[A](m: Manifest[A]): String = m.toString match {
case "Any" => "NOOOOOOOOOO"
case "java.lang.String" => "char*"
case "Char" => "char"
case "Array[Char]" => "char*"
case "Array[Double]" => "double*"
case "Array[Int]" => "int*"
case "Array[Long]" => "int64_t*"
case "Array[Float]" => "float*"
case "Array[Array[Int]]" => "int**"
case "Array[Array[Double]]" => "double**"
case "Array[Array[Float]]" => "float**"
case f if f.startsWith("scala.Function") =>
val targs = m.typeArguments.dropRight(1)
val res = remap(m.typeArguments.last)
// val targsUnboxed = targs.flatMap(t => unwrapTupleStr(remap(t)))
// val sep = if (targsUnboxed.length > 0) "," else ""
def remapInFunction[A](m: Manifest[A]): Array[String] = {
val s = m.toString
if (s.startsWith("scala.Tuple")) m.typeArguments.map(t => remap(t)).toArray
else scala.Array(remap(m))
}
val targsUnboxed = targs.flatMap(t => remapInFunction(t))
"function<" + res + "(" + targsUnboxed.mkString(",") + ")>"
// scala.Function1[Array[Double], Array[Double]] --> function<double*(double*)>
case _ => super.remap(m)
}
override def format(s: Exp[Any]): String = {
remap(s.tp) match {
case "uint16_t" => "%c"
case "bool" | "int8_t" | "int16_t" | "int32_t" => "%d"
case "int64_t" => "%ld"
case "float" | "double" => "%f"
case "string" => "%s"
case "char*" => "%s"
case "char" => "%c"
case "void" => "%c"
case _ =>
import scala.virtualization.lms.internal.GenerationFailedException
throw new GenerationFailedException("CGenMiscOps: cannot print type " + remap(s.tp))
}
}
override def quoteRawString(s: Exp[Any]): String = {
remap(s.tp) match {
case "string" => quote(s) + ".c_str()"
case _ => quote(s)
}
}
// we treat string as a primitive type to prevent memory management on strings
// strings are always stack allocated and freed automatically at the scope exit
override def isPrimitiveType(tpe: String) : Boolean = {
tpe match {
case "char*" => true
case "char" => true
case _ => super.isPrimitiveType(tpe)
}
}
// XX: from LMS 1.0
override def emitValDef(sym: Sym[Any], rhs: String): Unit = {
if (!isVoidType(sym.tp))
stream.println(remapWithRef(sym.tp) + quote(sym) + " = " + rhs + ";")
else // we might still want the RHS for its effects
stream.println(rhs + ";")
}
override def quote(x: Exp[Any]) = x match {
case Const(s: String) => "\""+s.replace("\"", "\\\"")+"\"" // TODO: more escapes?
case Const('\n') if x.tp == manifest[Char] => "'\\n'"
case Const('\t') if x.tp == manifest[Char] => "'\\t'"
case Const(0) if x.tp == manifest[Char] => "'\\0'"
case _ => super.quote(x)
}
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case CharToInt(s) => stream.println(s"int32_t ${quote{sym}} = (int32_t) ${quote(s)};")
case Error(s) => stream.println("assert(false && " + quote(s) + ");")
case afs@ArrayFromSeq(xs) =>
stream.println(remap(afs.m) + " " + quote(sym) + "[" + xs.length + "] = {" + (xs map quote mkString ",") + "};")
case a@ArrayNew(n) =>
val arrType = remap(a.m)
// emitValDef(sym, getMallocString(quote(n), arrType))
emitValDef(sym, getMallocArenaString(quote(n), arrType))
// stream.println("unique_ptr<" + arrType + "[]> " + quote(sym) + "(new " + arrType + "[" + quote(n) + "]);")
// stream.println("shared_ptr<" + arrType + "[]> " + quote(sym) + "(new " + arrType + "[" + quote(n) + "]);")
case ArrayApply(x,n) => emitValDef(sym, quote(x) + "[" + quote(n) + "]")
case ArrayUpdate(x,n,y) => stream.println(quote(x) + "[" + quote(n) + "] = " + quote(y) + ";")
case PrintLn(s) => stream.println("printf(\"" + format(s) + "\\n\"," + quoteRawString(s) + ");")
case StringCharAt(s,i) => emitValDef(sym, "%s[%s]".format(quote(s), quote(i)))
case RawCode(s) =>
stream.println(s)
case RawComment(s) =>
stream.println("// "+s)
case Comment(s, verbose, b) =>
stream.println("//#" + s)
if (verbose) {
stream.println("// generated code for " + s.replace('_', ' '))
} else {
stream.println("// generated code")
}
emitBlock(b)
emitValDef(sym, quote(getBlockResult(b)))
stream.println("//#" + s)
case MathTanh(x) => emitValDef(sym, src"tanh($x)")
// // add for fun with 6 or more parameters
// case FieldApply(UnboxedTuple(vars), "_6") => emitValDef(sym, quote(vars(5))){}
case _ => super.emitNode(sym,rhs)
}
// List of header files, to be imported in the code template.
def templateHeaders: Seq[String] = Seq(
"<assert.h>", "<err.h>", "<errno.h>", "<fcntl.h>", "<functional>",
"<math.h>", "<memory>", "<random>", "<stdint.h>", "<stdio.h>",
"<sys/mman.h>", "<sys/stat.h>", "<sys/time.h>", "<time.h>", "<unistd.h>", "<cblas.h>", "<algorithm>", "<numeric>")
// Raw code, to be included in the code template at file scope, before the main function.
def templateRawCode: String = ""
def preamble = raw"""
|using namespace std;
|#ifndef MAP_FILE
|#define MAP_FILE MAP_SHARED
|#endif
|
|long fsize(int fd) {
| struct stat stat;
| int res = fstat(fd, &stat);
| return stat.st_size;
|}
|
|int printll(char *s) {
| while (*s != '\n' && *s != ',' && *s != '\t') {
| putchar(*s++);
| }
| return 0;
|}
|
|long hash(char *str0, int len) {
| unsigned char *str = (unsigned char *)str0;
| unsigned long hash = 5381;
| int c;
|
| while ((c = *str++) && len--)
| hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
| return hash;
|}
|
|long HEAP_SIZE_CPU = 1073741826; // 1048576; // 536870912; // 268435456; // 2097152; 1610612739; // 4294967304; //
|void *mallocBase = calloc(HEAP_SIZE_CPU, 1);
|void *mallocAddr = mallocBase;
|void *waterMark = mallocBase;
|void *myMalloc(size_t bytes) {
| void *res = mallocAddr;
| mallocAddr = (void *)((char *)mallocAddr + bytes);
| if ((long)mallocAddr >= (long)mallocBase + HEAP_SIZE_CPU) {
| fprintf(stderr, "CPU memory breached limit of HEAP_SIZE_CPU\n"); abort();
| }
| return res;
|}
|
|long HEAP_SIZE = 8589934608; // 4294967304; // this is for GPU
|
|int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1) {
| long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
| result->tv_sec = diff / 1000000;
| result->tv_usec = diff % 1000000;
| return (diff < 0);
|}
|
|$templateRawCode
|
|void Snippet(char *);
|
|std::random_device rd{};
|std::mt19937 gen{rd()};
|std::normal_distribution<> d{0, 0.01};
|
|int main(int argc, char *argv[]) {
| if (argc != 2) {
| printf("usage: query <filename>\n");
| return 0;
| }
| Snippet(argv[1]);
| return 0;
|}
|""".stripMargin
override def emitSource[A:Manifest](args: List[Sym[_]], body: Block[A], functionName: String, out: java.io.PrintWriter) = {
withStream(out) {
stream.println(templateHeaders.map(x => s"#include $x").mkString("\n"))
stream.println(preamble)
}
super.emitSource[A](args, body, functionName, out)
}
}
trait DslGenC extends DslGenBase {
val IR: DslExp
import IR._
}
trait DslGenCublas extends DslGenBase with CudaGenGPUOps {
val IR: DslGPUExp
import IR._
override def templateHeaders: Seq[String] =
super.templateHeaders ++ Seq("<cuda.h>", "<cuda_runtime.h>", "<cublas_v2.h>")
override def templateRawCode: String = super.templateRawCode +
"""
|#define CUDA_CALL(f) { \
| cudaError_t err = (f); \
| if (err != cudaSuccess) { \
| fprintf(stderr, "CUDA error occurred: %s (%s:%d)\n", \
| cudaGetErrorString(err), __FILE__, __LINE__); \
| exit(err); \
| } \
|}
|
|#define CUBLAS_CALL(f) { \
| cublasStatus_t stat = (f); \
| if (stat != CUBLAS_STATUS_SUCCESS) { \
| fprintf(stderr, "cuBLAS error occurred: %d (%s:%d)\n", \
| stat, __FILE__, __LINE__); \
| exit(stat); \
| } \
|}
|
|void *gpuMallocBase;
|void *gpuMallocAddr;
|
|// Alignment boundary size, in bytes.
|constexpr int N = 4; // 16
|void *myGpuMalloc(size_t bytes) {
| bytes = ((bytes + (1 << N) - 1) >> N) << N;
| void *res = gpuMallocAddr;
| gpuMallocAddr = (void *)((char *)gpuMallocAddr + bytes);
| if ((long)gpuMallocAddr >= (long)gpuMallocBase + HEAP_SIZE) {
| fprintf(stderr, "GPU breached memory limit of HEAP_SIZE\n"); abort();
| }
| return res;
|}
|
|void myGpuFree(size_t bytes) {
| bytes = ((bytes + (1 << N) - 1) >> N) << N;
| gpuMallocAddr = (void *)((char *)gpuMallocAddr - bytes);
| cudaMemset((void*)gpuMallocAddr, 0, bytes);
| return;
|}
|
|template <typename T>
|__global__ void arrayUpdate(T *data, int index, T value) {
| data[index] = value;
|}
|
|__global__ void arrayFill(float* data, float value, int size) {
| int stride = gridDim.x * blockDim.x;
| int tid = threadIdx.x + blockIdx.x * blockDim.x;
| for (int i = tid; i < size; i += stride) data[i] = value;
|}
|
|__global__ void hardTanh(float* in, float* out, float min_val, float max_val, int size) {
| int tid = threadIdx.x + blockIdx.x * blockDim.x;
| int stride = gridDim.x * blockDim.x;
| for (int i = tid; i < size; i += stride) {
| out[i] = in[i] < min_val ? min_val : (in[i] > max_val ? max_val : in[i]);
| }
|}
|
|__global__ void hardTanh_grad(float* in_x, float* in_d, float* out_d, float min_val, float max_val, int size, bool inplace) {
| int tid = threadIdx.x + blockIdx.x * blockDim.x;
| int stride = gridDim.x * blockDim.x;
| for (int i = tid; i < size; i += stride) {
| if (inplace) {
| if (in_x[i] < min_val || in_x[i] > max_val) in_d[i] = 0;
| } else {
| if (in_x[i] >= min_val && in_x[i] <= max_val) in_d[i] += out_d[i];
| }
| }
|}
|
|__global__ void nllLoss(float *x, int x_stride, float *y, int* target) {
| int tid = threadIdx.x + blockIdx.x * blockDim.x;
| int offset = tid * x_stride + target[tid];
| y[tid] = -1 * x[offset];
|}
|
|__global__ void nllLoss_grad(int x_stride, float *yGrad, int* target, float* xGrad) {
| int tid = threadIdx.x + blockIdx.x * blockDim.x;
| int offset = tid * x_stride + target[tid];
| xGrad[offset] += -1 * yGrad[tid];
|}
|
| // only for 4D tensor in and 3D tensor out (TODO: incorrect!)
|__global__ void sum_optimization(float* in, int inStr0, int inStr1, int inStr2, int inStr3,
| float* out, int outStr0, int outStr1, int outStr2,
| int dim, int nElementOut, int dimSize) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (int i = tid; i < nElementOut; i += stride) {
| int outOff0 = i / outStr0;
| int outOff1temp = i - outOff0 * outStr0;
| int outOff1 = outOff1temp / outStr1;
| int outOff2 = outOff1temp - outOff1 * outStr1;
| for (int j = 0; j < dimSize; j++) {
| int inOff;
| if (dim == 0) inOff = j * inStr0 + outOff0 * inStr1 + outOff1 * inStr2 + outOff2 * inStr3;
| if (dim == 1) inOff = outOff0 * inStr0 + j * inStr1 + outOff1 * inStr2 + outOff2 * inStr3;
| if (dim == 2) inOff = outOff0 * inStr0 + outOff1 * inStr1 + j * inStr2 + outOff2 * inStr3;
| if (dim == 3) inOff = outOff0 * inStr0 + outOff1 * inStr1 + outOff2 * inStr2 + j * inStr3;
| out[i] += in[inOff];
| }
| }
|}
| // only for 4D tensor in and 3D tensor out
|__global__ void sum_grad(float* in, int inSize0, int inSize1, int inSize2, int inSize3, int nElement,
| float* out, int outStride0, int outStride1, int outStride2, int dim) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (int i = tid; i < nElement; i += stride) {
| int inOff2 = i / inSize3;
| int inDim3 = i - inOff2 * inSize3;
| int inOff1 = inOff2 / inSize2;
| int inDim2 = inOff2 - inOff1 * inSize2;
| int inDim0 = inOff1 / inSize1;
| int inDim1 = inOff1 - inDim0 * inSize1;
| int outOff = 0;
| if (dim == 0) outOff = inDim1 * outStride0 + inDim2 * outStride1 + inDim3 * outStride2;
| if (dim == 1) outOff = inDim0 * outStride0 + inDim2 * outStride1 + inDim3 * outStride2;
| if (dim == 2) outOff = inDim0 * outStride0 + inDim1 * outStride1 + inDim3 * outStride2;
| if (dim == 3) outOff = inDim0 * outStride0 + inDim1 * outStride1 + inDim2 * outStride2;
| in[i] += out[outOff];
| }
|}
|
|//following - https://github.com/torch/cutorch/blob/master/lib/THC/THCTensorMath.cuh#L49
|template <int Dims>
|static inline __device__ int compute(const int outputSizes[Dims], const int outputStrides[Dims],
| const int dimSize, const int concatDim, int linearIndex) {
| int offset = 0;
| #pragma unroll
| for (int i = Dims - 1; i >= 1; --i) {
| int curDimSize = i == concatDim? dimSize : outputSizes[i];
| int nextDimIndex = linearIndex / curDimSize;
| int curDimIndex = linearIndex - curDimSize * nextDimIndex;
| int curDimOffset = curDimIndex * outputStrides[i];
| offset += curDimOffset;
| linearIndex = nextDimIndex;
| }
| return offset + linearIndex * outputStrides[0];
|}
|
|// TODO: Only for Dim of rank 4, and only for 2 inputs
|__global__ void concat2D_1D_greg(float* in1, int dimSize1, int nElement1,
| float* in2, int dimSize2, int nElement2,
| float* out, int concatDim,
| int outSize0, int outSize1, int outSize2, int outSize3,
| int outStride0, int outStride1, int outStride2, int outStride3) {
| int outSizes[] = {outSize0, outSize1, outSize2, outSize3};
| int outStrides[] = {outStride0, outStride1, outStride2, outStride3};
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int nElement = blockIdx.y == 0 ? nElement1 : nElement2;
| if (tid >= nElement) return;
| float* data = blockIdx.y == 0 ? in1 : in2;
| int offset = blockIdx.y == 0 ? 0 : dimSize1;
| int dimSize = blockIdx.y == 0 ? dimSize1 : dimSize2;
| int dataOffset = offset * outStrides[concatDim];
| int stride = gridDim.x * blockDim.x;
| for (; tid < nElement; tid += stride) {
| int elementOffset = compute<4>(outSizes, //0, outSize1, outSize2, outSize3,
| outStrides, //0, outStride1, outStride2, outStride3,
| dimSize, concatDim, tid);
| out[dataOffset + elementOffset] = data[tid];
| }
|}
|
|// TODO: Only for Dim of rank 4, and only for 2 inputs, and only for concat at dim = 1
|__global__ void concat2D_1D_greg_grad(float* in1, int dimSize1, int nElement1,
| float* in2, int dimSize2, int nElement2,
| float* out, int concatDim,
| int outSize0, int outSize1, int outSize2, int outSize3,
| int outStride0, int outStride1, int outStride2, int outStride3) {
| int outSizes[] = {outSize0, outSize1, outSize2, outSize3};
| int outStrides[] = {outStride0, outStride1, outStride2, outStride3};
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int nElement = blockIdx.y == 0 ? nElement1 : nElement2;
| if (tid >= nElement) return;
| float* data = blockIdx.y == 0 ? in1 : in2;
| int offset = blockIdx.y == 0 ? 0 : dimSize1;
| int dimSize = blockIdx.y == 0 ? dimSize1 : dimSize2;
| int dataOffset = offset * outStride1;
| int stride = gridDim.x * blockDim.x;
| for (; tid < nElement; tid += stride) {
| int elementOffset = compute<4>(outSizes, //0, outSize1, outSize2, outSize3,
| outStrides, //0, outStride1, outStride2, outStride3,
| dimSize, concatDim, tid);
| data[tid] += out[dataOffset + elementOffset];
| }
|}
|
|__global__ void repeat0(float* in, float* out, int outStride0, int outStride1, int outScalarCount) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < outScalarCount; tid += stride) {
| int linearIndex = tid;
| int outIndex0 = linearIndex / outStride0;
| linearIndex = linearIndex - outIndex0 * outStride0;
| int outIndex1 = linearIndex / outStride1;
| int outIndex2 = linearIndex - outIndex1 * outStride1;
| int inIndex = outIndex2 + (outIndex0 + outIndex1) * outStride1;
| out[tid] = in[inIndex];
| }
|}
|
|__global__ void shift0(float* in, float* out, int inDim0, int inStride0, int inStride1, int inScalarCount) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < inScalarCount; tid += stride) {
| int linearIndex = tid;
| int inIndex0 = linearIndex / inStride0;
| linearIndex = linearIndex - inIndex0 * inStride0;
| int inIndex1 = linearIndex / inStride1;
| if (inIndex0 + inIndex1 >= inDim0) return;
| out[tid + inIndex1 * inStride0] = in[tid];
| }
|}
|
|__global__ void adagrad_update_1D_1D(float* x, float* d, float* m, float clip, float lr, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride) {
| if (d[tid] > clip) d[tid] = clip;
| if (d[tid] < -clip) d[tid] = -clip;
| m[tid] += d[tid] * d[tid];
| x[tid] -= lr * d[tid] / sqrt(m[tid] + 0.00000001);
| d[tid] = 0;
| }
|}
|
|__global__ void momentum_update_1D_1D(float* x, float* d, float* m, float learning_rate, float momentum, float gradClip, bool nesterov, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride) {
| float temp = d[tid];
| if (temp > gradClip) temp = gradClip;
| if (temp < -gradClip) temp = -gradClip;
| m[tid] *= momentum;
| m[tid] += temp;
| if (nesterov) { temp += momentum * m[tid]; }
| else { temp = m[tid]; }
| x[tid] -= learning_rate * temp;
| d[tid] = 0;
| }
|}
|
|__global__ void addScalarInArrayInPlace(float* in, float* add, float scale, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) in[tid] += add[0] * scale;
|}
|
|__global__ void addScalar(float* in, float* out, float add, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = in[tid] + add;
|}
|__global__ void minusScalar(float* in, float* out, float minus, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = in[tid] - minus;
|}
|__global__ void multScalar(float* in, float* out, float mult, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = in[tid] * mult;
|}
|__global__ void divScalar(float* in, float* out, float div, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = in[tid] / div;
|}
|
|__global__ void elementwise_1D_1D_mul(float* in1, float* in2, float* out, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = in1[tid] * in2[tid];
|}
|
|__global__ void elementwise_1D_1D_mul_mutate(float* in1, float* in2, float* out, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] += in1[tid] * in2[tid];
|}
|
|__global__ void elementwise_1D_1D_add(float* in1, float* in2, float* out, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = in1[tid] + in2[tid];
|}
|
|__global__ void elementwise_1D_1D_minus(float* in1, float* in2, float* out, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = in1[tid] - in2[tid];
|}
|
|__global__ void elementwise_1D_1D_div(float* in1, float* in2, float* out, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = in1[tid] / in2[tid];
|}
|
|__global__ void elementwise_1D_1D_exp(float* in, float* out, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = exp(in[tid]);
|}
|__global__ void elementwise_1D_1D_log(float* in, float* out, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = log(in[tid]);
|}
|__global__ void elementwise_1D_1D_sqrt(float* in, float* out, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = sqrt(in[tid]);
|}
|
|__global__ void elementwise_1D_1D_square(float* in, float* out, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) out[tid] = in[tid] * in[tid];
|}
|
|__global__ void elementwise_1D_1D_exp_grad(float* in_x, float* in_d, float* out_x, float * out_d, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) in_d[tid] += out_d[tid] * out_x[tid];
|}
|
|__global__ void elementwise_1D_1D_log_grad(float* in_x, float* in_d, float* out_x, float * out_d, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) in_d[tid] += out_d[tid] / in_x[tid];
|}
|
|__global__ void elementwise_1D_1D_sqrt_grad(float* in_x, float* in_d, float* out_x, float * out_d, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) in_d[tid] += out_d[tid] / out_x[tid] / 2;
|}
|
|__global__ void elementwise_1D_1D_square_grad(float* in_x, float* in_d, float* out_x, float * out_d, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) in_d[tid] += out_d[tid] * 2 * in_x[tid];
|}
|
|__global__ void clipAt(float* in, float bound, int size) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < size; tid += stride)
| if (tid < size) {
| if (in[tid] > bound) in[tid] = bound;
| if (in[tid] < -bound) in[tid] = -bound;
| }
|}
|
|__global__ void mask4D(float* in, int* mask, int xstrides0, int xstrides1, int xstrides2, int xstrides3, int scalarCount) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < scalarCount; tid += stride) {
| int linearIndex = tid;
| int xindex0 = linearIndex / xstrides0;
| linearIndex = linearIndex - xstrides0 * xindex0;
| int xindex1 = linearIndex / xstrides1;
| linearIndex = linearIndex - xstrides1 * xindex1;
| int xindex2 = linearIndex / xstrides2;
| int xindex3 = linearIndex - xstrides2 * xindex2;
| if (xindex3 >= mask[xindex0]) in[tid] = 0;
| }
|}
|
|__global__ void mul_sub(float* in1, float* in2, float* out, int in1ScalarCount, int in2ScalarCount) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < in1ScalarCount; tid += stride) {
| out[tid] = in1[tid] * in2[tid % in2ScalarCount];
| }
|}
|
|__global__ void mul_sub_grad(float* in1_x, float* in1_d, float* in2_x, float* in2_d, float* out, int in1ScalarCount, int in2ScalarCount) {
| int tid = blockIdx.x * blockDim.x + threadIdx.x;
| int stride = gridDim.x * blockDim.x;
| for (; tid < in1ScalarCount; tid += stride) {
| int index = tid % in2ScalarCount;
| in1_d[tid] += out[tid] * in2_x[index];
| in2_d[tid] = in1_x[tid] * out[tid]; // this is the temp array, need to be reduced!
| }
|}
|
|cudnnConvolutionFwdAlgo_t algo_0 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_0 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_0 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_0 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_0 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_0 = false;
|cudnnConvolutionFwdAlgo_t algo_1 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_1 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_1 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_1 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_1 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_1 = false;
|cudnnConvolutionFwdAlgo_t algo_2 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_2 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_2 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_2 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_2 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_2 = false;
|cudnnConvolutionFwdAlgo_t algo_3 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_3 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_3 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_3 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_3 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_3 = false;
|cudnnConvolutionFwdAlgo_t algo_4 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_4 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_4 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_4 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_4 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_4 = false;
|cudnnConvolutionFwdAlgo_t algo_5 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_5 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_5 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_5 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_5 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_5 = false;
|cudnnConvolutionFwdAlgo_t algo_6 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_6 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_6 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_6 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_6 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_6 = false;
|cudnnConvolutionFwdAlgo_t algo_7 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_7 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_7 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_7 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_7 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_7 = false;
|cudnnConvolutionFwdAlgo_t algo_8 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_8 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_8 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_8 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_8 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_8 = false;
|cudnnConvolutionFwdAlgo_t algo_9 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_9 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_9 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_9 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_9 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_9 = false;
|cudnnConvolutionFwdAlgo_t algo_10 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_10 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_10 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_10 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_10 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_10 = false;
|cudnnConvolutionFwdAlgo_t algo_11 = CUDNN_CONVOLUTION_FWD_ALGO_GEMM; bool init_algo_11 = false;
|cudnnConvolutionBwdDataAlgo_t algo_bwd_11 = CUDNN_CONVOLUTION_BWD_DATA_ALGO_0; bool init_algo_bwd_11 = false;
|cudnnConvolutionBwdFilterAlgo_t algo_bwf_11 = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0; bool init_algo_bwf_11 = false;
|
|""".stripMargin
}
trait DslGenCudnn extends DslGenCublas {
val IR: DslGPUExp
import IR._
override def templateHeaders: Seq[String] = super.templateHeaders ++ Seq("<cudnn.h>")
override def templateRawCode: String = super.templateRawCode +
"""
|#define CUDNN_CALL(f) { \
| cudnnStatus_t stat = (f); \
| if (stat != CUDNN_STATUS_SUCCESS) { \
| fprintf(stderr, "cuDNN error occurred: %d (%s:%d)\n", \
| stat, __FILE__, __LINE__); \
| exit(stat); \
| } \
|}
|""".stripMargin
}
abstract class DslDriverScala[A: Manifest, B: Manifest] extends DslOps with DslExp with CompileScala { self =>
val codegen = new DslGenScala {
val IR: self.type = self
}
def snippet(x: Rep[A]): Rep[B]
lazy val f = compile[A,B](snippet)
def precompile: Unit = f
// def precompileSilently: Unit = utils.devnull(f)
def eval(x: A): B = f(x)
lazy val code: String = {
val source = new java.io.StringWriter()
codegen.emitSource[A,B](snippet, "Snippet", new java.io.PrintWriter(source))
source.toString
}
}
abstract class DslDriverBase[A: Manifest, B: Manifest] extends DslExp { self =>
// The C-like code generator.
val codegen: DslGenBase {
val IR: self.type
}
val dir = "/tmp"
val fileName = s"lantern-snippet-${scala.util.Random.alphanumeric.take(4).mkString}"
// The code snippet to compile.
def snippet(x: Rep[A]): Rep[B]
def eval(a: A)
// Note: is it possible to implement `eval` returning `B`?
// def eval(a: A): B
lazy val code: String = {
val source = new java.io.StringWriter()
codegen.emitSource[A,B](snippet, "Snippet", new java.io.PrintWriter(source))
source.toString
}
}
abstract class DslDriverC[A: Manifest, B: Manifest] extends DslDriverBase[A, B] { self =>
override val codegen = new DslGenC {
val IR: self.type = self
}
override def eval(a: A) {
val cppFileName = s"$dir/$fileName.cpp"
val binaryFileName = s"$dir/$fileName"
val out = new java.io.PrintWriter(cppFileName)
out.println(code)
out.close()
new java.io.File(binaryFileName).delete
import scala.sys.process._
System.out.println("Compile C++ code")
(s"g++ -std=c++11 -O1 $cppFileName -o $binaryFileName -I /opt/OpenBLAS/include -L /opt/OpenBLAS/lib -lopenblas -lpthread": ProcessBuilder).lines.foreach(System.out.println) //-std=c99
System.out.println("Run C++ code")
(s"$binaryFileName $a": ProcessBuilder).lines.foreach(System.out.println)
}
}
abstract class DslDriverCuda[A: Manifest, B: Manifest] extends DslDriverBase[A, B] with DslGPUExp {
//def nvccArguments: Seq[String] = Seq("-ccbin gcc-5", "-std=c++11", "-O1", "--expt-extended-lambda", "-Wno-deprecated-gpu-targets", "-lstdc++")
def nvccArguments: Seq[String] = Seq("-std=c++11", "-O1", "--expt-extended-lambda", "-Wno-deprecated-gpu-targets", "-lstdc++")
}
abstract class DslDriverCublas[A: Manifest, B: Manifest] extends DslDriverCuda[A, B] { self =>
override val codegen = new DslGenCublas {
val IR: self.type = self
}
override def nvccArguments: Seq[String] = super.nvccArguments ++ Seq("-lcublas")
override def eval(a: A) {
val cudaFileName = s"$dir/$fileName.cu"
val binaryFileName = s"$dir/$fileName"
val out = new java.io.PrintWriter(cudaFileName)
out.println(code)
out.close()
new java.io.File(binaryFileName).delete
import scala.sys.process._
System.out.println("Compile C++ (cuBLAS) code")
(s"nvcc $cudaFileName -o $binaryFileName ${nvccArguments.mkString(" ")}": ProcessBuilder).lines.foreach(System.out.println) //-std=c99
System.out.println("Run C++ (cuBLAS) code")
(s"$binaryFileName $a": ProcessBuilder).lines.foreach(System.out.println)
}
}
abstract class DslDriverCudnn[A: Manifest, B: Manifest] extends DslDriverCuda[A, B] { self =>
override val codegen = new DslGenCudnn {
val IR: self.type = self
}