This file is indexed.

/usr/include/linbox/matrix/matrixdomain/opencl-domain-util.inl is in liblinbox-dev 1.4.2-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
/*
 * Copyright (C) 2012 Matthew Wezowicz
 *
 * Written by Matthew Wezowicz <mwezz@udel.edu>
 *
 * ========LICENCE========
 * This file is part of the library LinBox.
 *
 * LinBox is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 *.
 */

/** @internal
 * @file linbox/matrix/matrixdomain/opencl-domain-setup.inl
 * @brief NO DOC
 */


#ifndef __LINBOX_opencl_matrix_domain_util_INL
#define __LINBOX_opencl_matrix_domain_util_INL

#include <cstdio>

#include <utility>

#include "linbox/matrix/matrixdomain/opencl-domain-factory.h"

namespace LinBox{

	/**
	 * @internal
	 * Initializes the OpenCL compute environment
	 */
	template <class Field>
	void OpenCLMatrixDomain<Field>::oclMatrixDomainAcquire(){

		OpenCLMatrixDomainFactory::oclMatrixDomainInstance(this);
	}

	/**
	 * @internal
	 * Releases OpenCL cumpute resources
	 */
	template <class Field>
	void OpenCLMatrixDomain<Field>::oclMatrixDomainRelease(unsigned int IDnum_){

		OpenCLMatrixDomainFactory::oclMatrixDomainDeallocate(IDnum_);
	}

	/**
	 * @internal
	 * Checks to see if the memory levels required are possible
	 */
	template <>
	template <class Operand1, class Operand2, class Operand3>
	bool OpenCLMatrixDomain<Givaro::Modular<double> >::oclMemCheck(
		Operand1& D,
		const Operand2& A,
		const Operand3& B,
		const Operand1& C) const{

		//Calculate dimensions after padding of matrices
		//((A.coldim() / 16) + (A.coldim() % 16 == 0 ? 0 : 1)) * 16
		int newDDimX = (int) ((D.coldim() + 15) / 16) * 16;
		int newDDimY = (int) ((D.rowdim() + 15) / 16) * 16;
		int newADimX = (int) ((A.coldim() + 15) / 16) * 16;
		int newADimY = (int) ((A.rowdim() + 15) / 16) * 16;
		int newBDimX = (int) ((B.coldim() + 15) / 16) * 16;
		int newBDimY = (int) ((B.rowdim() + 15) / 16) * 16;
		int newCDimX = (int) ((C.coldim() + 15) / 16) * 16;
		int newCDimY = (int) ((C.rowdim() + 15) / 16) * 16;

		//Determine if each individual matrix will fit in a buffer
		bool temp = (maxBufferSize >= ((size_t)(newDDimX * newDDimY) * sizeof(cl_double)));
		temp &= (maxBufferSize >= (size_t)(newADimX * newADimY) * sizeof(cl_double));
		temp &= (maxBufferSize >= (size_t)(newBDimX * newBDimY) * sizeof(cl_double));
		temp &= (maxBufferSize >= (size_t)(newCDimX * newCDimY) * sizeof(cl_double));

		//Determine if all three buffers will fit at the same time
		temp &= (memCapacity >= ((size_t)(newDDimX * newDDimY) + (size_t)(newADimX * newADimY) +
			      (size_t)(newBDimX * newBDimY) + (size_t)(newCDimX * newCDimY)) * sizeof(cl_double));

		return temp;
	}

	template <>
	template <class Operand1, class Operand2, class Operand3>
	bool OpenCLMatrixDomain<Givaro::Modular<float> >::oclMemCheck(
		Operand1& D,
		const Operand2& A,
		const Operand3& B,
		const Operand1& C) const{

		//Calculate dimensions after padding of matrices
		//((A.coldim() / 16) + (A.coldim() % 16 == 0 ? 0 : 1)) * 16
		int newDDimX = (int) ((D.coldim() + 15) / 16) * 16;
		int newDDimY = (int) ((D.rowdim() + 15) / 16) * 16;
		int newADimX = (int) ((A.coldim() + 15) / 16) * 16;
		int newADimY = (int) ((A.rowdim() + 15) / 16) * 16;
		int newBDimX = (int) ((B.coldim() + 15) / 16) * 16;
		int newBDimY = (int) ((B.rowdim() + 15) / 16) * 16;
		int newCDimX = (int) ((C.coldim() + 15) / 16) * 16;
		int newCDimY = (int) ((C.rowdim() + 15) / 16) * 16;

		//Determine if each individual matrix will fit in a buffer
		bool temp = (maxBufferSize >= ((size_t)(newDDimX * newDDimY) * sizeof(cl_float)));
		temp &= (maxBufferSize >= (size_t)(newADimX * newADimY) * sizeof(cl_float));
		temp &= (maxBufferSize >= (size_t)(newBDimX * newBDimY) * sizeof(cl_float));
		temp &= (maxBufferSize >= (size_t)(newCDimX * newCDimY) * sizeof(cl_float));

		//Determine if all three buffers will fit at the same time
		temp &= (memCapacity >= ((size_t)(newDDimX * newDDimY) + (size_t)(newADimX * newADimY) +
			      (size_t)(newBDimX * newBDimY) + (size_t)(newCDimX * newCDimY)) * sizeof(cl_float));

		return temp;
	}

	template <>
	template <>
	bool OpenCLMatrixDomain<Givaro::Modular<double> >::oclMemCheck<std::pair<int,int> >(
		std::pair<int,int>& D,
		std::pair<int,int>& A,
		std::pair<int,int>& B,
		std::pair<int,int>& C) const{

		//Calculate dimensions after padding of matrices
		//((A.second / 16) + (A.second % 16 == 0 ? 0 : 1)) * 16
		int newDDimX = ((D.second + 15) / 16) * 16;
		int newDDimY = ((D.first + 15) / 16) * 16;
		int newADimX = ((A.second + 15) / 16) * 16;
		int newADimY = ((A.first + 15) / 16) * 16;
		int newBDimX = ((B.second + 15) / 16) * 16;
		int newBDimY = ((B.first + 15) / 16) * 16;
		int newCDimX = ((C.second + 15) / 16) * 16;
		int newCDimY = ((C.first + 15) / 16) * 16;

		//Determine if each individual matrix will fit in a buffer
		bool temp = (maxBufferSize >= (size_t)(newDDimX * newDDimY) * sizeof(cl_double));
		temp &= (maxBufferSize >= (size_t)(newADimX * newADimY) * sizeof(cl_double));
		temp &= (maxBufferSize >= (size_t)(newBDimX * newBDimY) * sizeof(cl_double));
		temp &= (maxBufferSize >= (size_t)(newCDimX * newCDimY) * sizeof(cl_double));

		//Determine if all three buffers will fit at the same time
		temp &= (memCapacity >= ((size_t)(newDDimX * newDDimY) + (size_t)(newADimX * newADimY) +
			      (size_t)(newBDimX * newBDimY) + (size_t)(newCDimX * newCDimY)) * sizeof(cl_double));

		return temp;
	}

	template <>
	template <>
	bool OpenCLMatrixDomain<Givaro::Modular<float> >::oclMemCheck<std::pair<int,int> >(
		std::pair<int,int>& D,
		std::pair<int,int>& A,
		std::pair<int,int>& B,
		std::pair<int,int>& C) const{

		//Calculate dimensions after padding of matrices
		//((A.second / 16) + (A.second % 16 == 0 ? 0 : 1)) * 16
		int newDDimX = ((D.second + 15) / 16) * 16;
		int newDDimY = ((D.first + 15) / 16) * 16;
		int newADimX = ((A.second + 15) / 16) * 16;
		int newADimY = ((A.first + 15) / 16) * 16;
		int newBDimX = ((B.second + 15) / 16) * 16;
		int newBDimY = ((B.first + 15) / 16) * 16;
		int newCDimX = ((C.second + 15) / 16) * 16;
		int newCDimY = ((C.first + 15) / 16) * 16;

		//Determine if each individual matrix will fit in a buffer
		bool temp = (maxBufferSize >= (size_t)(newDDimX * newDDimY) * sizeof(cl_float));
		temp &= (maxBufferSize >= (size_t)(newADimX * newADimY) * sizeof(cl_float));
		temp &= (maxBufferSize >= (size_t)(newBDimX * newBDimY) * sizeof(cl_float));
		temp &= (maxBufferSize >= (size_t) (newCDimX * newCDimY) * sizeof(cl_float));

		//Determine if all three buffers will fit at the same time
		temp &= (memCapacity >= ((size_t)(newDDimX * newDDimY) + (size_t)(newADimX * newADimY) +
			      (size_t)(newBDimX * newBDimY) + (size_t)(newCDimX * newCDimY)) * sizeof(cl_float));

		return temp;
	}

	/**
	 * @internal
	 * Functions to call the passed kernel on the passed buffers
	 */
	template <class Field>
	template <typename T, typename U>
	void OpenCLMatrixDomain<Field>::oclCallKernel(
		cl_mem bufferC,
		cl_mem bufferA,
		cl_mem bufferB,
		int widthA,
		int heightA,
		int widthB,
		T p,
		cl_kernel selectedKernel) const{

		//Pass kernel arguments
		cl_int tempErrcode;
		tempErrcode = clSetKernelArg(
			selectedKernel,
			0,
			sizeof(cl_mem),
			(void*)&bufferC);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			1,
			sizeof(cl_mem),
			(void*)&bufferA);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			2,
			sizeof(cl_mem),
			(void*)&bufferB);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			3,
			sizeof(cl_int),
			(void*)&widthA);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			4,
			sizeof(cl_int),
			(void*)&widthB);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(selectedKernel, 5, sizeof(U), (void*)&p);
		linbox_check(tempErrcode == CL_SUCCESS);


		//Set NDRange
		size_t localWorkSize[2];
		size_t globalWorkSize[2];
		localWorkSize[0] = 16;
		localWorkSize[1] = 16;
		globalWorkSize[0] = (size_t)widthB;
		globalWorkSize[1] = (size_t)heightA;

		//Launch kernel
		tempErrcode = clEnqueueNDRangeKernel(
			commandQue,
			selectedKernel,
			2,
			NULL,
			globalWorkSize,
			localWorkSize,
			0,
			NULL,
			NULL);
		linbox_check(tempErrcode == CL_SUCCESS);
	}

	template <class Field>
	template <typename T, typename U>
	void OpenCLMatrixDomain<Field>::oclCallKernel(
		cl_mem bufferD,
		cl_mem bufferA,
		cl_mem bufferB,
		cl_mem bufferC,
		int widthA,
		int heightA,
		int widthB,
		T p,
		cl_kernel selectedKernel) const{

		//Pass kernel arguments
		cl_int tempErrcode;
		tempErrcode = clSetKernelArg(
			selectedKernel,
			0,
			sizeof(cl_mem),
			(void*)&bufferD);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			1,
			sizeof(cl_mem),
			(void*)&bufferA);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			2,
			sizeof(cl_mem),
			(void*)&bufferB);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			3,
			sizeof(cl_mem),
			(void*)&bufferC);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			4,
			sizeof(cl_int),
			(void*)&widthA);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			5,
			sizeof(cl_int),
			(void*)&widthB);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(selectedKernel, 6, sizeof(U), (void*)&p);
		linbox_check(tempErrcode == CL_SUCCESS);

		//Set NDRange
		size_t localWorkSize[2];
		size_t globalWorkSize[2];
		localWorkSize[0] = 16;
		localWorkSize[1] = 16;
		globalWorkSize[0] = (size_t)widthB;
		globalWorkSize[1] = (size_t)heightA;

		//Launch kernel
		tempErrcode = clEnqueueNDRangeKernel(
			commandQue,
			selectedKernel,
			2,
			NULL,
			globalWorkSize,
			localWorkSize,
			0,
			NULL,
			NULL);
		linbox_check(tempErrcode == CL_SUCCESS);
	}

	template <class Field>
	template <typename T, typename U>
	void OpenCLMatrixDomain<Field>::oclCallKernel(
		cl_mem bufferD,
		cl_mem bufferA,
		cl_mem bufferB,
		cl_mem bufferC,
		T alpha,
		T beta,
		int widthA,
		int heightA,
		int widthB,
		T p,
		cl_kernel selectedKernel) const{

		//Pass kernel arguments
		cl_int tempErrcode;
		tempErrcode = clSetKernelArg(
			selectedKernel,
			0,
			sizeof(cl_mem),
			(void*)&bufferD);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			1,
			sizeof(U),
			(void*)&alpha);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			2,
			sizeof(cl_mem),
			(void*)&bufferA);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			3,
			sizeof(cl_mem),
			(void*)&bufferB);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			4,
			sizeof(U),
			(void*)&beta);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			5,
			sizeof(cl_mem),
			(void*)&bufferC);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			6,
			sizeof(cl_int),
			(void*)&widthA);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(
			selectedKernel,
			7,
			sizeof(cl_int),
			(void*)&widthB);
		linbox_check(tempErrcode == CL_SUCCESS);

		tempErrcode = clSetKernelArg(selectedKernel, 8, sizeof(U), (void*)&p);
		linbox_check(tempErrcode == CL_SUCCESS);


		//Set NDRange
		size_t localWorkSize[2];
		size_t globalWorkSize[2];
		localWorkSize[0] = 16;
		localWorkSize[1] = 16;
		globalWorkSize[0] = (size_t)widthB;
		globalWorkSize[1] = (size_t)heightA;

		//Launch kernel
		tempErrcode = clEnqueueNDRangeKernel(
			commandQue,
			selectedKernel,
			2,
			NULL,
			globalWorkSize,
			localWorkSize,
			0,
			NULL,
			NULL);
		linbox_check(tempErrcode == CL_SUCCESS);

	}


	/**
	 * @internal
	 * Functions to partition the matrices into submatrix views
	 */
	template <class Field>
	template <class Operand1, class Operand2, class Operand3>
	std::vector<int> OpenCLMatrixDomain<Field>::oclPartition(
		Operand1& C,
		const Operand2& A,
		const Operand3& B,
		std::vector<SubmatrixAdapter<Operand1> >& VC,
		std::vector<SubmatrixAdapter<Operand2> >& VA,
		std::vector<SubmatrixAdapter<Operand3> >& VB) const{

		std::vector<SubmatrixAdapter<Operand1> > VT;
		return oclPartition<Operand1,Operand2,Operand3>(C,A,B,C,VT,VA,VB,VC);
	}

	template <class Field>
	template <class Operand1, class Operand2, class Operand3>
	std::vector<int> OpenCLMatrixDomain<Field>::oclPartition(
		Operand1& D,
		const Operand2& A,
		const Operand3& B,
		const Operand1& C,
		std::vector<SubmatrixAdapter<Operand1> >& VD,
		std::vector<SubmatrixAdapter<Operand2> >& VA,
		std::vector<SubmatrixAdapter<Operand3> >& VB,
		std::vector<SubmatrixAdapter<Operand1> >& VC) const{

		//Compute if the OpenCL device is capable of working with the required ammounts of memory
		bool memLevelsAllowed = oclMemCheck<Operand1,Operand2,Operand3>(D,A,B,C);

		std::vector<int> temp;

		if(memLevelsAllowed){
			// Create Submatrix views
			SubmatrixAdapter<Operand1> SD(D);
			SubmatrixAdapter<Operand2> SA(A);
			SubmatrixAdapter<Operand3> SB(B);
			SubmatrixAdapter<Operand1> SC(C);

			// Place Submatrices at the beginning of the vectors
			VD.push_back(SD);
			VA.push_back(SA);
			VB.push_back(SB);
			VC.push_back(SC);

			// Return the block dimensions
			temp.push_back(1); //DBlocksX & CBlocksX
			temp.push_back(1); //DBlocksY & CBlocksY
			temp.push_back(1); //ABlocksY
			temp.push_back(1); //ABlocksY
			temp.push_back(1); //CBlocksY
			temp.push_back(1); //CBlocksY

			return temp;
		}

		//Begin search for Submatrices small enough to search on the device
		int DRows = (int) D.rowdim();
		int DCols = (int) D.coldim();
		int ARows = (int) A.rowdim();
		int ACols = (int) A.rowdim();
		int BRows = (int) B.rowdim();
		int BCols = (int) B.coldim();

		if(true){ //Default partitioning scheme

			//Loop until Submatrices that fit on the device have been found
			while(!memLevelsAllowed){
				int divisionFactor = 1;
				//Increase the number of subsections
				divisionFactor++;

				//Compute the number of rows and columns in each subsections
				int subRows = DRows / divisionFactor;
				int subCols = DCols / divisionFactor;
				int subSharedDim = ACols / divisionFactor;

				//Check if adjustment is need for some of the subsections
				bool addToSubRows = false;
				bool addToSubCols = false;
				bool addToSubSharedDim = false;

				if((subRows * divisionFactor) != DRows){
					addToSubRows = true;
				}
				if((subCols * divisionFactor) != DCols){
					addToSubCols = true;
				}
				if((subSharedDim * divisionFactor) != ACols){
					addToSubSharedDim = true;
				}

				//Determine of the largest subsections will fit on the device together
				int largestSubRows = (addToSubRows ? subRows : (subRows + 1));
				int largestSubCols = (addToSubCols ? subCols : (subCols + 1));
				int largestSubSharedDim = (addToSubSharedDim ? subSharedDim :
				                                               (subSharedDim + 1));

				std::pair<int,int> largestSubD(largestSubRows, largestSubCols);
				std::pair<int,int> largestSubA(largestSubRows, largestSubSharedDim);
				std::pair<int,int> largestSubB(largestSubSharedDim, largestSubCols);
				std::pair<int,int> largestSubC(largestSubRows, largestSubCols);

				memLevelsAllowed = oclMemCheck<std::pair<int,int> >(
					largestSubD,
					largestSubA,
					largestSubB,
					largestSubC);

				//If the largest subsections can fit on the device together
				//Begin partitioning the input matrices
				if(memLevelsAllowed){
					// Return the block dimensions
					temp.push_back(divisionFactor); //DBlocksX & CBlocksX
					temp.push_back(divisionFactor); //DBlocksY & CBlocksY
					temp.push_back(divisionFactor); //ABlocksY
					temp.push_back(divisionFactor); //ABlocksY
					temp.push_back(divisionFactor); //CBlocksY
					temp.push_back(divisionFactor); //CBlocksY

					//Loop through all but the last row
					for(int blockY = 0; blockY < (divisionFactor - 1); blockY++){
						int DRowsOffset = blockY * subRows;
						int ARowsOffset = blockY * subRows;
						int BRowsOffset = blockY * subSharedDim;

						//Loop through all but the last column
						for(int blockX = 0; blockX < (divisionFactor - 1); blockX++){
							int DColsOffset = blockX * subCols;
							int AColsOffset = blockX * subSharedDim;
							int BColsOffset = blockX * subCols;

							SubmatrixAdapter<Operand1> SD(
								D,
								(size_t)DRowsOffset,
								(size_t)DColsOffset,
								(size_t)subRows,
								(size_t)subCols);
							SubmatrixAdapter<Operand2> SA(
								A,
								(size_t)ARowsOffset,
								(size_t)AColsOffset,
								(size_t)subRows,
								(size_t)subSharedDim);
							SubmatrixAdapter<Operand3> SB(
								B,
								(size_t)BRowsOffset,
								(size_t)BColsOffset,
								(size_t)subSharedDim,
								(size_t)subCols);
							SubmatrixAdapter<Operand1> SC(
								C,
								(size_t)DRowsOffset,
								(size_t)DColsOffset,
								(size_t)subRows,
								(size_t)subCols);

							VD.push_back(SD);
							VA.push_back(SA);
							VB.push_back(SB);
							VC.push_back(SC);
						}

						int DColsOffset = (divisionFactor - 1) * subCols;
						int AColsOffset = (divisionFactor - 1) * subSharedDim;
						int BColsOffset = (divisionFactor - 1) * subCols;

						SubmatrixAdapter<Operand1> SD(
							D,
							(size_t)DRowsOffset,
							(size_t)DColsOffset,
							(size_t)subRows,
							(size_t)(DCols - DColsOffset));
						SubmatrixAdapter<Operand2> SA(
							A,
							(size_t)ARowsOffset,
							(size_t)AColsOffset,
							(size_t)subRows,
							(size_t)(ACols - AColsOffset));
						SubmatrixAdapter<Operand3> SB(
							B,
							(size_t)BRowsOffset,
							(size_t)BColsOffset,
							(size_t)subSharedDim,
							(size_t)(BCols - BColsOffset));
						SubmatrixAdapter<Operand1> SC(
							C,
							(size_t)DRowsOffset,
							(size_t)DColsOffset,
							(size_t)subRows,
							(size_t)(DCols - DColsOffset));

						VD.push_back(SD);
						VA.push_back(SA);
						VB.push_back(SB);
						VC.push_back(SC);
					}

					//Partition the last row
					int DRowsOffset = (divisionFactor - 1) * subRows;
					int ARowsOffset = (divisionFactor - 1) * subRows;
					int BRowsOffset = (divisionFactor - 1) * subSharedDim;

					for(int blockX = 0; blockX < (divisionFactor - 1); blockX++){
						int DColsOffset = blockX * subCols;
						int AColsOffset = blockX * subSharedDim;
						int BColsOffset = blockX * subCols;

						SubmatrixAdapter<Operand1> SD(
							D,
							(size_t)DRowsOffset,
							(size_t)DColsOffset,
							(size_t)(DRows - DRowsOffset),
							(size_t)subCols);
						SubmatrixAdapter<Operand2> SA(
							A,
							(size_t)ARowsOffset,
							(size_t)AColsOffset,
							(size_t)(ARows - ARowsOffset),
							(size_t)subSharedDim);
						SubmatrixAdapter<Operand3> SB(
							B,
							(size_t)BRowsOffset,
							(size_t)BColsOffset,
							(size_t)(BRows - BRowsOffset),
							(size_t)subCols);
						SubmatrixAdapter<Operand1> SC(
							C,
							(size_t)DRowsOffset,
							(size_t)DColsOffset,
							(size_t)(DRows - DRowsOffset),
							(size_t)subCols);

						VD.push_back(SD);
						VA.push_back(SA);
						VB.push_back(SB);
						VC.push_back(SC);
					}

					int DColsOffset = (divisionFactor - 1) * subCols;
					int AColsOffset = (divisionFactor - 1) * subSharedDim;
					int BColsOffset = (divisionFactor - 1) * subCols;

					SubmatrixAdapter<Operand1> SD(
						D,
						(size_t)DRowsOffset,
						(size_t)DColsOffset,
						(size_t)(DRows - DRowsOffset),
						(size_t)(DCols - DColsOffset));
					SubmatrixAdapter<Operand2> SA(
						A,
						(size_t)ARowsOffset,
						(size_t)AColsOffset,
						(size_t)(ARows - ARowsOffset),
						(size_t)(ACols - AColsOffset));
					SubmatrixAdapter<Operand3> SB(
						B,
						(size_t)BRowsOffset,
						(size_t)BColsOffset,
						(size_t)(BRows - BRowsOffset),
						(size_t)(BCols - BColsOffset));
					SubmatrixAdapter<Operand1> SC(
						C,
						(size_t)DRowsOffset,
						(size_t)DColsOffset,
						(size_t)(DRows - DRowsOffset),
						(size_t)(DCols - DColsOffset));

					VD.push_back(SD);
					VA.push_back(SA);
					VB.push_back(SB);
					VC.push_back(SC);

				}
			}
		}

		return temp;
	}

	//Prints the appropriate error message
	template <class Field>
	void OpenCLMatrixDomain<Field>::printClErrstring(cl_int err) const{
		switch (err) {
		case CL_SUCCESS:
			std::cout << "Success!\n";
			break;
		case CL_DEVICE_NOT_FOUND:
			std::cout << "Device not found\n";
			break;
		case CL_DEVICE_NOT_AVAILABLE:
			std::cout << "Device not available\n";
			break;
		case CL_COMPILER_NOT_AVAILABLE:
			std::cout << "Compiler not available\n";
			break;
		case CL_MEM_OBJECT_ALLOCATION_FAILURE:
			std::cout << "Memory object allocation failure\n";
			break;
		case CL_OUT_OF_RESOURCES:
			std::cout << "Out of resources\n";
			break;
		case CL_OUT_OF_HOST_MEMORY:
			std::cout << "Out of host memory\n";
			break;
		case CL_PROFILING_INFO_NOT_AVAILABLE:
			std::cout << "Profiling information not available\n";
			break;
		case CL_MEM_COPY_OVERLAP:
			std::cout << "Memory copy overlap\n";
			break;
		case CL_IMAGE_FORMAT_MISMATCH:
			std::cout << "Image format mismatch\n";
			break;
		case CL_IMAGE_FORMAT_NOT_SUPPORTED:
			std::cout << "Image format not supported\n";
			break;
		case CL_BUILD_PROGRAM_FAILURE:
			std::cout << "Program build failure\n";
			break;
		case CL_MAP_FAILURE:
			std::cout << "Map failure\n";
			break;
		case CL_INVALID_VALUE:
			std::cout << "Invalid value\n";
			break;
		case CL_INVALID_DEVICE_TYPE:
			std::cout << "Invalid device type\n";
			break;
		case CL_INVALID_PLATFORM:
			std::cout << "Invalid platform\n";
			break;
		case CL_INVALID_DEVICE:
			std::cout << "Invalid device\n";
			break;
		case CL_INVALID_CONTEXT:
			std::cout << "Invalid context\n";
			break;
		case CL_INVALID_QUEUE_PROPERTIES:
			std::cout << "Invalid queue properties\n";
			break;
		case CL_INVALID_COMMAND_QUEUE:
			std::cout << "Invalid command queue\n";
			break;
		case CL_INVALID_HOST_PTR:
			std::cout << "Invalid host pointer\n";
			break;
		case CL_INVALID_MEM_OBJECT:
			std::cout << "Invalid memory object\n";
			break;
		case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
			std::cout << "Invalid image format descriptor\n";
			break;
		case CL_INVALID_IMAGE_SIZE:
			std::cout << "Invalid image size\n";
			break;
		case CL_INVALID_SAMPLER:
			std::cout << "Invalid sampler\n";
			break;
		case CL_INVALID_BINARY:
			std::cout << "Invalid binary\n";
			break;
		case CL_INVALID_BUILD_OPTIONS:
			std::cout << "Invalid build options\n";
			break;
		case CL_INVALID_PROGRAM:
			std::cout << "Invalid program\n";
			break;
		case CL_INVALID_PROGRAM_EXECUTABLE:
			std::cout << "Invalid program executable\n";
			break;
		case CL_INVALID_KERNEL_NAME:
			std::cout << "Invalid kernel name\n";
			break;
		case CL_INVALID_KERNEL_DEFINITION:
			std::cout << "Invalid kernel definition\n";
			break;
		case CL_INVALID_KERNEL:
			std::cout << "Invalid kernel\n";
			break;
		case CL_INVALID_ARG_INDEX:
			std::cout << "Invalid argument index\n";
			break;
		case CL_INVALID_ARG_VALUE:
			std::cout << "Invalid argument value\n";
			break;
		case CL_INVALID_ARG_SIZE:
			std::cout << "Invalid argument size\n";
			break;
		case CL_INVALID_KERNEL_ARGS:
			std::cout << "Invalid kernel arguments\n";
			break;
		case CL_INVALID_WORK_DIMENSION:
			std::cout << "Invalid work dimension\n";
			break;
		case CL_INVALID_WORK_GROUP_SIZE:
			std::cout << "Invalid work group size\n";
			break;
		case CL_INVALID_WORK_ITEM_SIZE:
			std::cout << "Invalid work item size\n";
			break;
		case CL_INVALID_GLOBAL_OFFSET:
			std::cout << "Invalid global offset\n";
			break;
		case CL_INVALID_EVENT_WAIT_LIST:
			std::cout << "Invalid event wait list\n";
			break;
		case CL_INVALID_EVENT:
			std::cout << "Invalid event\n";
			break;
		case CL_INVALID_OPERATION:
			std::cout << "Invalid operation\n";
			break;
		case CL_INVALID_GL_OBJECT:
			std::cout << "Invalid OpenGL object\n";
			break;
		case CL_INVALID_BUFFER_SIZE:
			std::cout << "Invalid buffer size\n";
			break;
		case CL_INVALID_MIP_LEVEL:
			std::cout << "Invalid mip-map level\n";
			break;
		default:
			std::cout << "Unknown\n";
			break;
		}
	}

} //end of namespace LinBox

#endif // __LINBOX_opencl_matrix_domain_util_INL

// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,:0,t0,+0,=s
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End: