-
Notifications
You must be signed in to change notification settings - Fork 8
/
gpu.go
1677 lines (1474 loc) · 55.7 KB
/
gpu.go
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
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package axon
import (
"embed"
"fmt"
"math"
"unsafe"
"cogentcore.org/core/base/mpi"
"cogentcore.org/core/system"
"cogentcore.org/core/vgpu"
vk "github.com/goki/vulkan"
)
//go:embed shaders/*.spv
var content embed.FS
//go:generate gosl -exclude=Update,UpdateParams,Defaults,AllParams,ShouldShow cogentcore.org/core/math32/fastexp.go cogentcore.org/core/math32/minmax ../chans/chans.go ../chans ../kinase ../fsfffb/inhib.go ../fsfffb github.com/emer/emergent/v2/etime github.com/emer/emergent/v2/ringidx rand.go avgmax.go neuromod.go globals.go context.go neuron.go synapse.go pool.go layervals.go act.go act_path.go inhib.go learn.go layertypes.go layerparams.go deep_layers.go rl_layers.go rubicon_layers.go pcore_layers.go pathtypes.go pathparams.go deep_paths.go rl_paths.go rubicon_paths.go pcore_paths.go hip_paths.go gpu_hlsl
// Full vars code -- each gpu_*.hlsl uses a subset
/*
// note: binding is var, set
// Set 0: uniform layer params -- could not have paths also be uniform..
[[vk::binding(0, 0)]] StructuredBuffer<LayerParams> Layers; // [Layer]
[[vk::binding(1, 0)]] StructuredBuffer<PathParams> Paths; // [Layer][SendPaths]
// Set 1: effectively uniform indexes and path params as structured buffers in storage
[[vk::binding(0, 1)]] StructuredBuffer<uint> NeuronIxs; // [Neurons][Indexes]
[[vk::binding(1, 1)]] StructuredBuffer<uint> SynapseIxs; // [Layer][SendPaths][SendNeurons][Syns]
[[vk::binding(2, 1)]] StructuredBuffer<StartN> SendCon; // [Layer][SendPaths][SendNeurons]
[[vk::binding(3, 1)]] StructuredBuffer<uint> RecvPathIndexes; // [Layer][RecvPaths]
[[vk::binding(4, 1)]] StructuredBuffer<StartN> RecvCon; // [Layer][RecvPaths][RecvNeurons]
[[vk::binding(5, 1)]] StructuredBuffer<uint> RecvSynIndexes; // [Layer][RecvPaths][RecvNeurons][Syns]
// Set 2: main network structs and vals -- all are writable
[[vk::binding(0, 2)]] RWStructuredBuffer<Context> Ctx; // [0]
[[vk::binding(1, 2)]] RWStructuredBuffer<float> Neurons; // [Neurons][Vars][Data]
[[vk::binding(2, 2)]] RWStructuredBuffer<float> NeuronAvgs; // [Neurons][Vars]
[[vk::binding(3, 2)]] RWStructuredBuffer<Pool> Pools; // [Layer][Pools][Data]
[[vk::binding(4, 2)]] RWStructuredBuffer<LayerValues> LayValues; // [Layer][Data]
[[vk::binding(5, 2)]] RWStructuredBuffer<float> Globals; // [NGlobals]
[[vk::binding(6, 2)]] RWStructuredBuffer<float> Exts; // [In / Out Layers][Neurons][Data]
// There might be a limit of 8 buffers per set -- can't remember..
// Set 3: synapse vars
[[vk::binding(0, 3)]] RWStructuredBuffer<int> GBuf; // [Layer][RecvPaths][RecvNeurons][MaxDel+1][Data]
[[vk::binding(1, 3)]] RWStructuredBuffer<float> GSyns; // [Layer][RecvPaths][RecvNeurons][Data]
[[vk::binding(2, 3)]] RWStructuredBuffer<float> Synapses; // [Layer][SendPaths][SendNeurons][Syns]
// todo: future expansion to add more tranches of Synapses
// Set 4: SynCa -- can only access in 2^31 chunks
[[vk::binding(0, 4)]] RWStructuredBuffer<float> SynapseCas; // [Layer][SendPaths][SendNeurons][Syns][Data]
[[vk::binding(1, 4)]] RWStructuredBuffer<float> SynapseCas1; // [Layer][SendPaths][SendNeurons][Syns][Data]
[[vk::binding(2, 4)]] RWStructuredBuffer<float> SynapseCas2; // [Layer][SendPaths][SendNeurons][Syns][Data]
[[vk::binding(3, 4)]] RWStructuredBuffer<float> SynapseCas3; // [Layer][SendPaths][SendNeurons][Syns][Data]
[[vk::binding(4, 4)]] RWStructuredBuffer<float> SynapseCas4; // [Layer][SendPaths][SendNeurons][Syns][Data]
[[vk::binding(5, 4)]] RWStructuredBuffer<float> SynapseCas5; // [Layer][SendPaths][SendNeurons][Syns][Data]
[[vk::binding(6, 4)]] RWStructuredBuffer<float> SynapseCas6; // [Layer][SendPaths][SendNeurons][Syns][Data]
[[vk::binding(7, 4)]] RWStructuredBuffer<float> SynapseCas7; // [Layer][SendPaths][SendNeurons][Syns][Data]
Set: 0
Role: Storage
Var: 0: Layers Struct[4] (size: 1520) Values: 1
Var: 1: Paths Struct[5] (size: 352) Values: 1
Set: 1
Role: Storage
Var: 0: NeuronIxs Uint32[534] (size: 4) Values: 1
Var: 1: SynapseIxs Uint32[38976] (size: 4) Values: 1
Var: 2: SendCon Struct[242] (size: 16) Values: 1
Var: 3: RecvPathIndexes Uint32[5] (size: 4) Values: 1
Var: 4: RecvCon Struct[281] (size: 16) Values: 1
Var: 5: RecvSynIndexes Uint32[12992] (size: 4) Values: 1
Set: 2
Role: Storage
Var: 0: Ctx Struct (size: 512) Values: 1
Var: 1: Neurons Float32[227840] (size: 4) Values: 1
Var: 2: NeuronAvgs Float32[1246] (size: 4) Values: 1
Var: 3: Pools Struct[64] (size: 1040) Values: 1
Var: 4: LayValues Struct[64] (size: 80) Values: 1
Var: 5: Globals Float32[976] (size: 4) Values: 1
Var: 6: Exts Float32[800] (size: 4) Values: 1
Set: 3
Role: Storage
Var: 0: GBuf Int32[13488] (size: 4) Values: 1
Var: 1: GSyns Float32[4496] (size: 4) Values: 1
Var: 2: Synapses Float32[64960] (size: 4) Values: 1
Set: 4
Role: Storage
Var: 0: SynapseCas0 Float32[1455104] (size: 4) Values: 1
Var: 1: SynapseCas1 Float32 (size: 4) Values: 1
Var: 2: SynapseCas2 Float32 (size: 4) Values: 1
Var: 3: SynapseCas3 Float32 (size: 4) Values: 1
Var: 4: SynapseCas4 Float32 (size: 4) Values: 1
Var: 5: SynapseCas5 Float32 (size: 4) Values: 1
Var: 6: SynapseCas6 Float32 (size: 4) Values: 1
*/
// TheGPU is the gpu device, shared across all networks
var TheGPU *vgpu.GPU
// CyclesN is the number of cycles to run as a group
// for ra25, 10 = ~50 msec / trial, 25 = ~48, all 150 / 50 minus / plus = ~44
// 10 is good enough and unlikely to mess with anything else..
const CyclesN = 10
// PushOff has push constants for setting offset into compute shader
type PushOff struct {
// offset
Off uint32
pad, pad1, pad2 uint32
}
// GPU manages all of the GPU-based computation for a given Network.
// Lives within the network.
type GPU struct {
// if true, actually use the GPU
On bool
// if true, slower separate shader pipeline runs are used, with a CPU-sync Wait at the end, to enable timing information about each individual shader to be collected using the network FunTimer system. otherwise, only aggregate information is available about the entire Cycle call.
RecFunTimes bool
// if true, process each cycle one at a time. Otherwise, 10 cycles at a time are processed in one batch.
CycleByCycle bool
// the network we operate on -- we live under this net
Net *Network `view:"-"`
// the context we use
Ctx *Context `view:"-"`
// the vgpu compute system
Sys *vgpu.System `view:"-"`
// VarSet = 0: the uniform LayerParams
Params *vgpu.VarSet `view:"-"`
// VarSet = 1: the storage indexes and PathParams
Indexes *vgpu.VarSet `view:"-"`
// VarSet = 2: the Storage buffer for RW state structs and neuron floats
Structs *vgpu.VarSet `view:"-"`
// Varset = 3: the Storage buffer for synapses
Syns *vgpu.VarSet `view:"-"`
// Varset = 4: the Storage buffer for SynCa banks
SynCas *vgpu.VarSet `view:"-"`
// for sequencing commands
Semaphores map[string]vk.Semaphore `view:"-"`
// number of warp threads -- typically 64 -- must update all hlsl files if changed!
NThreads int `view:"-" inactive:"-" default:"64"`
// maximum number of bytes per individual storage buffer element, from GPUProps.Limits.MaxStorageBufferRange
MaxBufferBytes uint32 `view:"-"`
// bank of floats for GPU access
SynapseCas0 []float32 `view:"-"`
// bank of floats for GPU access
SynapseCas1 []float32 `view:"-"`
// bank of floats for GPU access
SynapseCas2 []float32 `view:"-"`
// bank of floats for GPU access
SynapseCas3 []float32 `view:"-"`
// bank of floats for GPU access
SynapseCas4 []float32 `view:"-"`
// bank of floats for GPU access
SynapseCas5 []float32 `view:"-"`
// bank of floats for GPU access
SynapseCas6 []float32 `view:"-"`
// bank of floats for GPU access
SynapseCas7 []float32 `view:"-"`
// tracks var binding
DidBind map[string]bool `view:"-"`
}
// ConfigGPUwithGUI turns on GPU mode in context of an active GUI where Vulkan
// has been initialized etc.
// Configures the GPU -- call after Network is Built, initialized, params are set,
// and everything is ready to run.
func (nt *Network) ConfigGPUwithGUI(ctx *Context) {
system.TheApp.RunOnMain(func() {
nt.GPU.Config(ctx, nt)
})
fmt.Printf("Running on GPU: %s\n", TheGPU.DeviceName)
}
// ConfigGPUnoGUI turns on GPU mode in case where no GUI is being used.
// This directly accesses the GPU hardware. It does not work well when GUI also being used.
// Configures the GPU -- call after Network is Built, initialized, params are set,
// and everything is ready to run.
func (nt *Network) ConfigGPUnoGUI(ctx *Context) {
if TheGPU == nil {
if err := vgpu.InitNoDisplay(); err != nil {
panic(err)
}
}
nt.GPU.Config(ctx, nt)
mpi.AllPrintf("Running on GPU: %s\n", TheGPU.DeviceName)
}
// Destroy should be called to release all the resources allocated by the network
func (gp *GPU) Destroy() {
if gp.Sys != nil {
gp.Sys.Destroy()
}
gp.Sys = nil
}
// Config configures the network -- must call on an already-built network
func (gp *GPU) Config(ctx *Context, net *Network) {
gp.On = true
gp.Net = net
gp.Ctx = ctx
gp.NThreads = 64
gp.DidBind = make(map[string]bool)
if TheGPU == nil {
TheGPU = vgpu.NewComputeGPU()
// vgpu.Debug = true
opts := vgpu.NewRequiredOpts(vgpu.OptShaderInt64) // , vgpu.OptRobustBufferAccess
TheGPU.Config("axon", &opts)
}
gp.MaxBufferBytes = TheGPU.GPUProperties.Limits.MaxStorageBufferRange - 16
gp.Sys = TheGPU.NewComputeSystem("axon")
gp.Sys.StaticVars = true // no diff in perf..
gp.ConfigSynCaBuffs()
vars := gp.Sys.Vars()
pcset := vars.AddPushSet()
gp.Params = vars.AddSet()
gp.Indexes = vars.AddSet()
gp.Structs = vars.AddSet()
gp.Syns = vars.AddSet()
gp.SynCas = vars.AddSet()
pcset.AddStruct("PushOff", int(unsafe.Sizeof(PushOff{})), 1, vgpu.Push, vgpu.ComputeShader)
gp.Params.AddStruct("Layers", int(unsafe.Sizeof(LayerParams{})), len(gp.Net.LayParams), vgpu.Storage, vgpu.ComputeShader)
gp.Params.AddStruct("Paths", int(unsafe.Sizeof(PathParams{})), len(gp.Net.PathParams), vgpu.Storage, vgpu.ComputeShader)
// note: paths must be in Storage here because couldn't have both Layers and Paths as uniform.
gp.Indexes.Add("NeuronIxs", vgpu.Uint32, len(gp.Net.NeuronIxs), vgpu.Storage, vgpu.ComputeShader)
gp.Indexes.Add("SynapseIxs", vgpu.Uint32, len(gp.Net.SynapseIxs), vgpu.Storage, vgpu.ComputeShader)
gp.Indexes.AddStruct("SendCon", int(unsafe.Sizeof(StartN{})), len(gp.Net.PathSendCon), vgpu.Storage, vgpu.ComputeShader)
gp.Indexes.Add("RecvPathIndexes", vgpu.Uint32, len(gp.Net.RecvPathIndexes), vgpu.Storage, vgpu.ComputeShader)
gp.Indexes.AddStruct("RecvCon", int(unsafe.Sizeof(StartN{})), len(gp.Net.PathRecvCon), vgpu.Storage, vgpu.ComputeShader)
gp.Indexes.Add("RecvSynIndexes", vgpu.Uint32, len(gp.Net.RecvSynIndexes), vgpu.Storage, vgpu.ComputeShader)
gp.Structs.AddStruct("Ctx", int(unsafe.Sizeof(Context{})), 1, vgpu.Storage, vgpu.ComputeShader)
gp.Structs.Add("Neurons", vgpu.Float32, len(gp.Net.Neurons), vgpu.Storage, vgpu.ComputeShader)
gp.Structs.Add("NeuronAvgs", vgpu.Float32, len(gp.Net.NeuronAvgs), vgpu.Storage, vgpu.ComputeShader)
gp.Structs.AddStruct("Pools", int(unsafe.Sizeof(Pool{})), len(gp.Net.Pools), vgpu.Storage, vgpu.ComputeShader)
gp.Structs.AddStruct("LayValues", int(unsafe.Sizeof(LayerValues{})), len(gp.Net.LayValues), vgpu.Storage, vgpu.ComputeShader)
gp.Structs.Add("Globals", vgpu.Float32, len(gp.Net.Globals), vgpu.Storage, vgpu.ComputeShader)
gp.Structs.Add("Exts", vgpu.Float32, len(gp.Net.Exts), vgpu.Storage, vgpu.ComputeShader)
gp.Syns.Add("GBuf", vgpu.Int32, len(gp.Net.PathGBuf), vgpu.Storage, vgpu.ComputeShader)
gp.Syns.Add("GSyns", vgpu.Float32, len(gp.Net.PathGSyns), vgpu.Storage, vgpu.ComputeShader)
gp.Syns.Add("Synapses", vgpu.Float32, len(gp.Net.Synapses), vgpu.Storage, vgpu.ComputeShader)
gp.SynCas.Add("SynapseCas0", vgpu.Float32, len(gp.SynapseCas0), vgpu.Storage, vgpu.ComputeShader)
gp.SynCas.Add("SynapseCas1", vgpu.Float32, len(gp.SynapseCas1), vgpu.Storage, vgpu.ComputeShader)
gp.SynCas.Add("SynapseCas2", vgpu.Float32, len(gp.SynapseCas2), vgpu.Storage, vgpu.ComputeShader)
gp.SynCas.Add("SynapseCas3", vgpu.Float32, len(gp.SynapseCas3), vgpu.Storage, vgpu.ComputeShader)
gp.SynCas.Add("SynapseCas4", vgpu.Float32, len(gp.SynapseCas4), vgpu.Storage, vgpu.ComputeShader)
gp.SynCas.Add("SynapseCas5", vgpu.Float32, len(gp.SynapseCas5), vgpu.Storage, vgpu.ComputeShader)
gp.SynCas.Add("SynapseCas6", vgpu.Float32, len(gp.SynapseCas6), vgpu.Storage, vgpu.ComputeShader)
gp.SynCas.Add("SynapseCas7", vgpu.Float32, len(gp.SynapseCas7), vgpu.Storage, vgpu.ComputeShader)
gp.Params.ConfigValues(1)
gp.Indexes.ConfigValues(1)
gp.Structs.ConfigValues(1)
gp.Syns.ConfigValues(1)
gp.SynCas.ConfigValues(1)
// pipelines
gp.Sys.NewComputePipelineEmbed("GatherSpikes", content, "shaders/gpu_gather.spv")
gp.Sys.NewComputePipelineEmbed("LayGi", content, "shaders/gpu_laygi.spv")
gp.Sys.NewComputePipelineEmbed("BetweenGi", content, "shaders/gpu_betweengi.spv")
gp.Sys.NewComputePipelineEmbed("PoolGi", content, "shaders/gpu_poolgi.spv")
gp.Sys.NewComputePipelineEmbed("Cycle", content, "shaders/gpu_cycle.spv")
gp.Sys.NewComputePipelineEmbed("CycleInc", content, "shaders/gpu_cycleinc.spv")
gp.Sys.NewComputePipelineEmbed("SendSpike", content, "shaders/gpu_sendspike.spv")
gp.Sys.NewComputePipelineEmbed("SynCa", content, "shaders/gpu_synca.spv")
gp.Sys.NewComputePipelineEmbed("CyclePost", content, "shaders/gpu_cyclepost.spv")
gp.Sys.NewComputePipelineEmbed("NewStatePool", content, "shaders/gpu_newstate_pool.spv")
gp.Sys.NewComputePipelineEmbed("NewStateNeuron", content, "shaders/gpu_newstate_neuron.spv")
gp.Sys.NewComputePipelineEmbed("MinusPool", content, "shaders/gpu_minuspool.spv")
gp.Sys.NewComputePipelineEmbed("MinusNeuron", content, "shaders/gpu_minusneuron.spv")
gp.Sys.NewComputePipelineEmbed("PlusStart", content, "shaders/gpu_plusstart.spv")
gp.Sys.NewComputePipelineEmbed("PlusPool", content, "shaders/gpu_pluspool.spv")
gp.Sys.NewComputePipelineEmbed("PlusNeuron", content, "shaders/gpu_plusneuron.spv")
gp.Sys.NewComputePipelineEmbed("DWt", content, "shaders/gpu_dwt.spv")
gp.Sys.NewComputePipelineEmbed("DWtFromDi", content, "shaders/gpu_dwtfmdi.spv")
gp.Sys.NewComputePipelineEmbed("WtFromDWt", content, "shaders/gpu_wtfmdwt.spv")
gp.Sys.NewComputePipelineEmbed("DWtSubMean", content, "shaders/gpu_dwtsubmean.spv")
gp.Sys.NewComputePipelineEmbed("ApplyExts", content, "shaders/gpu_applyext.spv")
gp.Sys.NewComputePipelineEmbed("TestSynCa", content, "shaders/gpu_test_synca.spv")
gp.Sys.Config()
gp.CopyParamsToStaging()
gp.CopyIndexesToStaging()
gp.CopyExtsToStaging()
gp.CopyContextToStaging()
gp.CopyStateToStaging()
gp.CopySynapsesToStaging()
gp.CopySynCaToStaging()
gp.Sys.Mem.SyncToGPU()
}
// ConfigSynCaBuffs configures special SynapseCas buffers needed for larger memory access
func (gp *GPU) ConfigSynCaBuffs() {
bufMax := gp.MaxBufferBytes
floatMax := int(bufMax) / 4 // 32 bit floats for now
ctx := gp.Ctx
net := gp.Net
ctx.NetIndexes.GPUMaxBuffFloats = uint32(floatMax)
net.Ctx.NetIndexes.GPUMaxBuffFloats = uint32(floatMax)
nSynCaFloat := len(net.SynapseCas)
nCaBanks := nSynCaFloat / int(floatMax)
caLast := nSynCaFloat % int(floatMax)
if caLast > 0 {
nCaBanks++
}
ctx.NetIndexes.GPUSynCaBanks = uint32(nCaBanks)
net.Ctx.NetIndexes.GPUSynCaBanks = uint32(nCaBanks)
// fmt.Printf("banks %d: MaxBuffFloats: %X\n", ctx.NetIndexes.GPUSynCaBanks, ctx.NetIndexes.GPUMaxBuffFloats)
if nCaBanks > 8 {
panic(fmt.Sprintf("SynapseCas only supports 8 banks of %X floats -- needs: %d banks\n", floatMax, nCaBanks))
}
base := 0
if nCaBanks > 1 {
gp.SynapseCas0 = net.SynapseCas[base : base+floatMax]
} else if nCaBanks == 1 {
gp.SynapseCas0 = net.SynapseCas[base : base+caLast]
}
base += floatMax
if nCaBanks > 2 {
gp.SynapseCas1 = net.SynapseCas[base : base+floatMax]
} else if nCaBanks == 2 {
gp.SynapseCas1 = net.SynapseCas[base : base+caLast]
} else {
gp.SynapseCas1 = make([]float32, 4) // dummy
}
base += floatMax
if nCaBanks > 3 {
gp.SynapseCas2 = net.SynapseCas[base : base+floatMax]
} else if nCaBanks == 3 {
gp.SynapseCas2 = net.SynapseCas[base : base+caLast]
} else {
gp.SynapseCas2 = make([]float32, 4) // dummy
}
base += floatMax
if nCaBanks > 4 {
gp.SynapseCas3 = net.SynapseCas[base : base+floatMax]
} else if nCaBanks == 4 {
gp.SynapseCas3 = net.SynapseCas[base : base+caLast]
} else {
gp.SynapseCas3 = make([]float32, 4) // dummy
}
base += floatMax
if nCaBanks > 5 {
gp.SynapseCas4 = net.SynapseCas[base : base+floatMax]
} else if nCaBanks == 5 {
gp.SynapseCas4 = net.SynapseCas[base : base+caLast]
} else {
gp.SynapseCas4 = make([]float32, 4) // dummy
}
base += floatMax
if nCaBanks > 6 {
gp.SynapseCas5 = net.SynapseCas[base : base+floatMax]
} else if nCaBanks == 6 {
gp.SynapseCas5 = net.SynapseCas[base : base+caLast]
} else {
gp.SynapseCas5 = make([]float32, 4) // dummy
}
base += floatMax
if nCaBanks > 7 {
gp.SynapseCas6 = net.SynapseCas[base : base+floatMax]
} else if nCaBanks == 7 {
gp.SynapseCas6 = net.SynapseCas[base : base+caLast]
} else {
gp.SynapseCas6 = make([]float32, 4) // dummy
}
base += floatMax
if nCaBanks > 8 {
gp.SynapseCas7 = net.SynapseCas[base : base+floatMax]
} else if nCaBanks == 8 {
gp.SynapseCas7 = net.SynapseCas[base : base+caLast]
} else {
gp.SynapseCas7 = make([]float32, 4) // dummy
}
}
///////////////////////////////////////////////////////////////////////
// Sync To
// the CopyFromBytes call automatically flags updated regions
// and SyncMemToGPU does all updated
// SyncMemToGPU synchronizes any staging memory buffers that have been updated with
// a Copy function, actually sending the updates from the staging -> GPU.
// The CopyTo commands just copy Network-local data to a staging buffer,
// and this command then actually moves that onto the GPU.
// In unified GPU memory architectures, this staging buffer is actually the same
// one used directly by the GPU -- otherwise it is a separate staging buffer.
func (gp *GPU) SyncMemToGPU() {
gp.Sys.Mem.SyncToGPU()
}
// CopyParamsToStaging copies the LayerParams and PathParams to staging from CPU.
// Must call SyncMemToGPU after this (see SyncParamsToGPU).
func (gp *GPU) CopyParamsToStaging() {
if !gp.On {
return
}
_, layv, _ := gp.Params.ValueByIndexTry("Layers", 0)
layv.CopyFromBytes(unsafe.Pointer(&gp.Net.LayParams[0]))
_, pjnv, _ := gp.Params.ValueByIndexTry("Paths", 0)
pjnv.CopyFromBytes(unsafe.Pointer(&gp.Net.PathParams[0]))
}
// SyncParamsToGPU copies the LayerParams and PathParams to the GPU from CPU.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
func (gp *GPU) SyncParamsToGPU() {
if !gp.On {
return
}
gp.CopyParamsToStaging()
gp.SyncMemToGPU()
}
// CopyIndexesToStaging is only called when the network is built
// to copy the indexes specifying connectivity etc to staging from CPU.
func (gp *GPU) CopyIndexesToStaging() {
if !gp.On {
return
}
_, neuriv, _ := gp.Indexes.ValueByIndexTry("NeuronIxs", 0)
neuriv.CopyFromBytes(unsafe.Pointer(&gp.Net.NeuronIxs[0]))
_, syniv, _ := gp.Indexes.ValueByIndexTry("SynapseIxs", 0)
syniv.CopyFromBytes(unsafe.Pointer(&gp.Net.SynapseIxs[0]))
_, sconv, _ := gp.Indexes.ValueByIndexTry("SendCon", 0)
sconv.CopyFromBytes(unsafe.Pointer(&gp.Net.PathSendCon[0]))
_, spiv, _ := gp.Indexes.ValueByIndexTry("RecvPathIndexes", 0)
spiv.CopyFromBytes(unsafe.Pointer(&gp.Net.RecvPathIndexes[0]))
_, rconv, _ := gp.Indexes.ValueByIndexTry("RecvCon", 0)
rconv.CopyFromBytes(unsafe.Pointer(&gp.Net.PathRecvCon[0]))
_, ssiv, _ := gp.Indexes.ValueByIndexTry("RecvSynIndexes", 0)
ssiv.CopyFromBytes(unsafe.Pointer(&gp.Net.RecvSynIndexes[0]))
}
// CopyExtsToStaging copies external inputs to staging from CPU.
// Typically used in RunApplyExts which also does the Sync.
func (gp *GPU) CopyExtsToStaging() {
if !gp.On {
return
}
_, extv, _ := gp.Structs.ValueByIndexTry("Exts", 0)
extv.CopyFromBytes(unsafe.Pointer(&gp.Net.Exts[0]))
}
// CopyContextToStaging copies current context to staging from CPU.
// Must call SyncMemToGPU after this (see SyncContextToGPU).
// See SetContext if there is a new one.
func (gp *GPU) CopyContextToStaging() {
if !gp.On {
return
}
_, ctxv, _ := gp.Structs.ValueByIndexTry("Ctx", 0)
_, glbv, _ := gp.Structs.ValueByIndexTry("Globals", 0)
ctxv.CopyFromBytes(unsafe.Pointer(gp.Ctx))
glbv.CopyFromBytes(unsafe.Pointer(&gp.Net.Globals[0]))
}
// SyncContextToGPU copies current context to GPU from CPU.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
// See SetContext if there is a new one.
func (gp *GPU) SyncContextToGPU() {
if !gp.On {
return
}
gp.CopyContextToStaging()
gp.SyncMemToGPU()
}
// SetContext sets our context to given context and syncs it to the GPU.
// Typically a single context is used as it must be synced into the GPU.
// The GPU never writes to the CPU
func (gp *GPU) SetContext(ctx *Context) {
if !gp.On {
return
}
gp.Ctx = ctx
gp.SyncContextToGPU()
}
// CopyLayerValuesToStaging copies LayerValues to staging from CPU.
// Must call SyncMemToGPU after this (see SyncLayerValuesToGPU).
func (gp *GPU) CopyLayerValuesToStaging() {
if !gp.On {
return
}
_, layv, _ := gp.Structs.ValueByIndexTry("LayValues", 0)
layv.CopyFromBytes(unsafe.Pointer(&gp.Net.LayValues[0]))
}
// SyncLayerValuesToGPU copies LayerValues to GPU from CPU.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
func (gp *GPU) SyncLayerValuesToGPU() {
if !gp.On {
return
}
gp.CopyLayerValuesToStaging()
gp.SyncMemToGPU()
}
// CopyPoolsToStaging copies Pools to staging from CPU.
// Must call SyncMemToGPU after this (see SyncPoolsToGPU).
func (gp *GPU) CopyPoolsToStaging() {
if !gp.On {
return
}
_, poolv, _ := gp.Structs.ValueByIndexTry("Pools", 0)
poolv.CopyFromBytes(unsafe.Pointer(&gp.Net.Pools[0]))
}
// SyncPoolsToGPU copies Pools to GPU from CPU.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
func (gp *GPU) SyncPoolsToGPU() {
if !gp.On {
return
}
gp.CopyPoolsToStaging()
gp.SyncMemToGPU()
}
// CopyNeuronsToStaging copies neuron state up to staging from CPU.
// Must call SyncMemToGPU after this (see SyncNeuronsToGPU).
func (gp *GPU) CopyNeuronsToStaging() {
if !gp.On {
return
}
_, neurv, _ := gp.Structs.ValueByIndexTry("Neurons", 0)
neurv.CopyFromBytes(unsafe.Pointer(&gp.Net.Neurons[0]))
_, neurav, _ := gp.Structs.ValueByIndexTry("NeuronAvgs", 0)
neurav.CopyFromBytes(unsafe.Pointer(&gp.Net.NeuronAvgs[0]))
}
// SyncNeuronsToGPU copies neuron state up to GPU from CPU.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
func (gp *GPU) SyncNeuronsToGPU() {
if !gp.On {
return
}
gp.CopyNeuronsToStaging()
gp.SyncMemToGPU()
}
// CopyStateToStaging copies LayerValues, Pools, Neurons state to staging from CPU.
// this is typically sufficient for most syncing --
// only missing the Synapses which must be copied separately.
// Must call SyncMemToGPU after this (see SyncStateToGPU).
func (gp *GPU) CopyStateToStaging() {
if !gp.On {
return
}
gp.CopyLayerValuesToStaging()
gp.CopyPoolsToStaging()
gp.CopyNeuronsToStaging()
}
// SyncStateToGPU copies LayValues, Pools, Neurons state to GPU
// this is typically sufficient for most syncing --
// only missing the Synapses which must be copied separately.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
func (gp *GPU) SyncStateToGPU() {
if !gp.On {
return
}
gp.CopyStateToStaging()
gp.SyncMemToGPU()
}
// SyncStateGBufToGPU copies LayValues, Pools, Neurons, GBuf state to GPU
// this is typically sufficient for most syncing --
// only missing the Synapses which must be copied separately.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
func (gp *GPU) SyncStateGBufToGPU() {
if !gp.On {
return
}
gp.CopyStateToStaging()
gp.CopyGBufToStaging()
gp.SyncMemToGPU()
}
// SyncAllToGPU copies LayerValues, Pools, Neurons, Synapses to GPU.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
func (gp *GPU) SyncAllToGPU() {
if !gp.On {
return
}
gp.CopyStateToStaging()
gp.CopySynapsesToStaging()
gp.SyncMemToGPU()
}
// CopySynapsesToStaging copies the synapse memory to staging (large).
// Does not copy SynCa synapse state -- see SynCa methods.
// This is not typically needed except when weights are initialized or
// for the Slow weight update processes that are not on GPU.
// Must call SyncMemToGPU after this (see SyncSynapsesToGPU).
func (gp *GPU) CopySynapsesToStaging() {
if !gp.On {
return
}
_, synv, _ := gp.Syns.ValueByIndexTry("Synapses", 0)
synv.CopyFromBytes(unsafe.Pointer(&gp.Net.Synapses[0]))
}
// SyncSynapsesToGPU copies the synapse memory to GPU (large).
// This is not typically needed except when weights are initialized or
// for the Slow weight update processes that are not on GPU.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
func (gp *GPU) SyncSynapsesToGPU() {
if !gp.On {
return
}
gp.CopySynapsesToStaging()
gp.SyncMemToGPU()
}
// CopySynCaToStaging copies the SynCa variables to GPU, which are per-Di (even larger).
// This is only used for initialization -- SynCa vars otherwise managed entirely on GPU.
// Must call SyncMemToGPU after this (see SyncSynCaToGPU).
func (gp *GPU) CopySynCaToStaging() {
if !gp.On {
return
}
// note: do not need these except in GUI or tests
_, syncv, _ := gp.SynCas.ValueByIndexTry("SynapseCas0", 0)
syncv.CopyFromBytes(unsafe.Pointer(&gp.SynapseCas0[0]))
if gp.Ctx.NetIndexes.GPUSynCaBanks > 1 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas1", 0)
syncv.CopyFromBytes(unsafe.Pointer(&gp.SynapseCas1[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 2 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas2", 0)
syncv.CopyFromBytes(unsafe.Pointer(&gp.SynapseCas2[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 3 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas3", 0)
syncv.CopyFromBytes(unsafe.Pointer(&gp.SynapseCas3[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 4 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas4", 0)
syncv.CopyFromBytes(unsafe.Pointer(&gp.SynapseCas4[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 5 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas5", 0)
syncv.CopyFromBytes(unsafe.Pointer(&gp.SynapseCas5[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 6 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas6", 0)
syncv.CopyFromBytes(unsafe.Pointer(&gp.SynapseCas6[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 7 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas7", 0)
syncv.CopyFromBytes(unsafe.Pointer(&gp.SynapseCas7[0]))
}
}
// SyncSynCaToGPU copies the SynCa variables to GPU, which are per-Di (even larger).
// This is only used for initialization -- SynCa vars otherwise managed entirely on GPU.
// Calls SyncMemToGPU -- use when this is the only copy taking place.
func (gp *GPU) SyncSynCaToGPU() {
if !gp.On {
return
}
gp.CopySynCaToStaging()
gp.SyncMemToGPU()
}
// CopyGBufToStaging copies the GBuf and GSyns memory to staging.
func (gp *GPU) CopyGBufToStaging() {
if !gp.On {
return
}
_, gbv, _ := gp.Syns.ValueByIndexTry("GBuf", 0)
gbv.CopyFromBytes(unsafe.Pointer(&gp.Net.PathGBuf[0]))
_, gsv, _ := gp.Syns.ValueByIndexTry("GSyns", 0)
gsv.CopyFromBytes(unsafe.Pointer(&gp.Net.PathGSyns[0]))
}
// SyncGBufToGPU copies the GBuf and GSyns memory to the GPU.
func (gp *GPU) SyncGBufToGPU() {
if !gp.On {
return
}
gp.CopyGBufToStaging()
gp.SyncMemToGPU()
}
///////////////////////////////////////////////////////////////////////
// Sync From
// unlike Sync To, need to specify the regions to sync from first
// and then copy from staging to get into CPU memory
// SyncRegionStruct returns the SyncRegion with error panic
func (gp *GPU) SyncRegionStruct(vnm string) vgpu.MemReg {
r, err := gp.Sys.Mem.SyncRegionValueIndex(gp.Structs.Set, vnm, 0)
if err != nil {
panic(err)
}
return r
}
// SyncRegionSyns returns the SyncRegion with error panic
func (gp *GPU) SyncRegionSyns(vnm string) vgpu.MemReg {
r, err := gp.Sys.Mem.SyncRegionValueIndex(gp.Syns.Set, vnm, 0)
if err != nil {
panic(err)
}
return r
}
// SyncRegionSynCas returns the SyncRegion with error panic
func (gp *GPU) SyncRegionSynCas(vnm string) vgpu.MemReg {
r, err := gp.Sys.Mem.SyncRegionValueIndex(gp.SynCas.Set, vnm, 0)
if err != nil {
panic(err)
}
return r
}
// CopyContextFromStaging copies Context from staging to CPU, after Sync back down.
func (gp *GPU) CopyContextFromStaging() {
if !gp.On {
return
}
_, ctxv, _ := gp.Structs.ValueByIndexTry("Ctx", 0)
_, glbv, _ := gp.Structs.ValueByIndexTry("Globals", 0)
ctxv.CopyToBytes(unsafe.Pointer(gp.Ctx))
glbv.CopyToBytes(unsafe.Pointer(&gp.Net.Globals[0]))
}
// SyncContextFromGPU copies Context from GPU to CPU.
// This is done at the end of each cycle to get state back from GPU for CPU-side computations.
// Use only when only thing being copied -- more efficient to get all at once.
// e.g. see SyncStateFromGPU
func (gp *GPU) SyncContextFromGPU() {
if !gp.On {
return
}
cxr := gp.SyncRegionStruct("Ctx")
glr := gp.SyncRegionStruct("Globals")
gp.Sys.Mem.SyncStorageRegionsFromGPU(cxr, glr)
gp.CopyContextFromStaging()
}
// CopyLayerValuesFromStaging copies LayerValues from staging to CPU, after Sync back down.
func (gp *GPU) CopyLayerValuesFromStaging() {
if !gp.On {
return
}
_, layv, _ := gp.Structs.ValueByIndexTry("LayValues", 0)
layv.CopyToBytes(unsafe.Pointer(&gp.Net.LayValues[0]))
}
// SyncLayerValuesFromGPU copies LayerValues from GPU to CPU.
// This is done at the end of each cycle to get state back from staging for CPU-side computations.
// Use only when only thing being copied -- more efficient to get all at once.
// e.g. see SyncStateFromGPU
func (gp *GPU) SyncLayerValuesFromGPU() {
if !gp.On {
return
}
lvr := gp.SyncRegionStruct("LayValues")
gp.Sys.Mem.SyncStorageRegionsFromGPU(lvr)
gp.CopyLayerValuesFromStaging()
}
// CopyPoolsFromStaging copies Pools from staging to CPU, after Sync back down.
func (gp *GPU) CopyPoolsFromStaging() {
if !gp.On {
return
}
_, plv, _ := gp.Structs.ValueByIndexTry("Pools", 0)
plv.CopyToBytes(unsafe.Pointer(&gp.Net.Pools[0]))
}
// SyncPoolsFromGPU copies Pools from GPU to CPU.
// Use only when only thing being copied -- more efficient to get all at once.
// e.g. see SyncStateFromGPU
func (gp *GPU) SyncPoolsFromGPU() {
if !gp.On {
return
}
plr := gp.SyncRegionStruct("Pools")
gp.Sys.Mem.SyncStorageRegionsFromGPU(plr)
gp.CopyPoolsFromStaging()
}
// CopyNeuronsFromStaging copies Neurons from staging to CPU, after Sync back down.
func (gp *GPU) CopyNeuronsFromStaging() {
if !gp.On {
return
}
_, neurv, _ := gp.Structs.ValueByIndexTry("Neurons", 0)
neurv.CopyToBytes(unsafe.Pointer(&gp.Net.Neurons[0]))
_, neurav, _ := gp.Structs.ValueByIndexTry("NeuronAvgs", 0)
neurav.CopyToBytes(unsafe.Pointer(&gp.Net.NeuronAvgs[0]))
// note: don't need to get indexes back down
}
// SyncNeuronsFromGPU copies Neurons from GPU to CPU.
// Use only when only thing being copied -- more efficient to get all at once.
// e.g. see SyncStateFromGPU
func (gp *GPU) SyncNeuronsFromGPU() {
if !gp.On {
return
}
nrr := gp.SyncRegionStruct("Neurons")
nrar := gp.SyncRegionStruct("NeuronAvgs")
// note: don't need to get indexes back down
gp.Sys.Mem.SyncStorageRegionsFromGPU(nrr, nrar)
gp.CopyNeuronsFromStaging()
}
// CopySynapsesFromStaging copies Synapses from staging to CPU, after Sync back down.
// Does not copy SynCa synapse state -- see SynCa methods.
func (gp *GPU) CopySynapsesFromStaging() {
if !gp.On {
return
}
_, synv, _ := gp.Syns.ValueByIndexTry("Synapses", 0)
synv.CopyToBytes(unsafe.Pointer(&gp.Net.Synapses[0]))
}
// SyncSynapsesFromGPU copies Synapses from GPU to CPU.
// Does not copy SynCa synapse state -- see SynCa methods.
// Use only when only thing being copied -- more efficient to get all at once.
func (gp *GPU) SyncSynapsesFromGPU() {
if !gp.On {
return
}
syr := gp.SyncRegionSyns("Synapses")
gp.Sys.Mem.SyncStorageRegionsFromGPU(syr)
gp.CopySynapsesFromStaging()
}
// CopySynCaFromStaging copies the SynCa variables to GPU, which are per-Di (even larger).
// This is only used for GUI viewing -- SynCa vars otherwise managed entirely on GPU.
func (gp *GPU) CopySynCaFromStaging() {
if !gp.On {
return
}
_, syncv, _ := gp.SynCas.ValueByIndexTry("SynapseCas0", 0)
syncv.CopyToBytes(unsafe.Pointer(&gp.SynapseCas0[0]))
if gp.Ctx.NetIndexes.GPUSynCaBanks > 1 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas1", 0)
syncv.CopyToBytes(unsafe.Pointer(&gp.SynapseCas1[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 2 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas2", 0)
syncv.CopyToBytes(unsafe.Pointer(&gp.SynapseCas2[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 3 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas3", 0)
syncv.CopyToBytes(unsafe.Pointer(&gp.SynapseCas3[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 4 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas4", 0)
syncv.CopyToBytes(unsafe.Pointer(&gp.SynapseCas4[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 5 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas5", 0)
syncv.CopyToBytes(unsafe.Pointer(&gp.SynapseCas5[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 6 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas6", 0)
syncv.CopyToBytes(unsafe.Pointer(&gp.SynapseCas6[0]))
}
if gp.Ctx.NetIndexes.GPUSynCaBanks > 7 {
_, syncv, _ = gp.SynCas.ValueByIndexTry("SynapseCas7", 0)
syncv.CopyToBytes(unsafe.Pointer(&gp.SynapseCas7[0]))
}
}
func (gp *GPU) SynCaBuff(idx uint32) []float32 {
switch idx {
case 0:
return gp.SynapseCas0
case 1:
return gp.SynapseCas1
case 2:
return gp.SynapseCas2
case 3:
return gp.SynapseCas3
case 4:
return gp.SynapseCas4
case 5:
return gp.SynapseCas5
case 6:
return gp.SynapseCas6
case 7:
return gp.SynapseCas7
}
return nil
}
// SyncSynCaFromGPU copies the SynCa variables to GPU, which are per-Di (even larger).
// This is only used for GUI viewing -- SynCa vars otherwise managed entirely on GPU.
// Use only when only thing being copied -- more efficient to get all at once.
func (gp *GPU) SyncSynCaFromGPU() {
if !gp.On {
return
}
ctx := gp.Ctx
nBanks := int(ctx.NetIndexes.GPUSynCaBanks)
regs := make([]vgpu.MemReg, nBanks)
for i := range regs {
reg := fmt.Sprintf("SynapseCas%d", i)
regs[i] = gp.SyncRegionSynCas(reg)
}
gp.Sys.Mem.SyncStorageRegionsFromGPU(regs...)
gp.CopySynCaFromStaging()
}
// CopyLayerStateFromStaging copies Context, LayerValues and Pools from staging to CPU, after Sync.
func (gp *GPU) CopyLayerStateFromStaging() {
gp.CopyContextFromStaging()
gp.CopyLayerValuesFromStaging()
gp.CopyPoolsFromStaging()
}
// SyncLayerStateFromCPU copies Context, LayerValues, and Pools from GPU to CPU.
// This is the main GPU->CPU sync step automatically called after each Cycle.
func (gp *GPU) SyncLayerStateFromGPU() {
if !gp.On {
return
}
cxr := gp.SyncRegionStruct("Ctx")
glr := gp.SyncRegionStruct("Globals")
lvr := gp.SyncRegionStruct("LayValues")
plr := gp.SyncRegionStruct("Pools")
gp.Sys.Mem.SyncStorageRegionsFromGPU(cxr, glr, lvr, plr)
gp.CopyLayerStateFromStaging()
}
// CopyStateFromStaging copies Context, LayerValues, Pools, and Neurons from staging to CPU, after Sync.
func (gp *GPU) CopyStateFromStaging() {
gp.CopyLayerStateFromStaging()
gp.CopyNeuronsFromStaging()
}
// SyncStateFromCPU copies Neurons, LayerValues, and Pools from GPU to CPU.
// This is the main GPU->CPU sync step automatically called in PlusPhase.