summaryrefslogtreecommitdiff
path: root/benchmarks/CUDA/NQU/nqueen.cu
blob: 68dc9b520427581c44b075780c773fc84db5649f (plain)
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
// N-queen for CUDA
//
// Copyright(c) 2008 Ping-Che Chen


//#define WIN32_LEAN_AND_MEAN
//#include <windows.h>
#include <stdio.h>
#include <cutil.h>

#define THREAD_NUM		96


int bunk = 0;		// this is a dummy variable used for making sure clock() are not optimized out

/*
 * ----------------------------------------------------------------
 * This is a recursive version of n-queen backtracking solver.
 * A non-recursive version is used instead.
 * ----------------------------------------------------------------

long long solve_nqueen_internal(int n, unsigned int mask, unsigned int l_mask, unsigned int r_mask, unsigned int t_mask)
{
	if(mask == t_mask) {
		return 1;
	}

	unsigned int m = (mask | l_mask | r_mask);
	if((m & t_mask) == t_mask) {
		return 0;
	}

	long long total = 0;
	unsigned int index = (m + 1) & ~m;
	while((index & t_mask) != 0) {
		total += solve_nqueen_internal(mask | index, (l_mask | index) << 1, (r_mask | index) >> 1, t_mask);
		m |= index;
		index = (m + 1) & ~m;
	}

	return total;
}


long long solve_nqueen(int n)
{
	return solve_nqueen_internal(0, 0, 0, (1 << n) - 1);
}
*/

/* -------------------------------------------------------------------
 * This is a non-recursive version of n-queen backtracking solver.
 * This provides the basis for the CUDA version.
 * -------------------------------------------------------------------
 */

long long solve_nqueen(int n)
{
	unsigned int mask[32];
	unsigned int l_mask[32];
	unsigned int r_mask[32];
	unsigned int m[32];

	if(n <= 0 || n > 32) {
		return 0;
	}

	const unsigned int t_mask = (1 << n) - 1;
	long long total = 0;
	long long upper_total = 0;
	int i = 0, j;
	unsigned int index;

	mask[0] = 0;
	l_mask[0] = 0;
	r_mask[0] = 0;
	m[0] = 0;

	for(j = 0; j < (n + 1) / 2; j++) {
		index = (1 << j);
		m[0] |= index;
		
		mask[1] = index;
		l_mask[1] = index << 1;
		r_mask[1] = index >> 1;
		m[1] = (mask[1] | l_mask[1] | r_mask[1]);
		i = 1;
		
		if(n % 2 == 1 && j == (n + 1) / 2 - 1) {
			upper_total = total;
			total = 0;
		}
		
		while(i > 0) {
			if((m[i] & t_mask) == t_mask) {
				i--;
			}
			else {
				index = ((m[i] + 1) ^ m[i]) & ~m[i];
				m[i] |= index;
				if((index & t_mask) != 0) {
					if(i + 1 == n) {
						total++;
						i--;
					}
					else {
						mask[i + 1] = mask[i] | index;
						l_mask[i + 1] = (l_mask[i] | index) << 1;
						r_mask[i + 1] = (r_mask[i] | index) >> 1;
						m[i + 1] = (mask[i + 1] | l_mask[i + 1] | r_mask[i + 1]);
						i++;
					}
				}
				else {
					i --;
				}
			}
		}
	}

	bunk = 2;

	if(n % 2 == 0) {
		return total * 2;
	}
	else {
		return upper_total * 2 + total;
	}
}


/* -------------------------------------------------------------------
 * This is a non-recursive version of n-queen backtracking solver
 * with multi-thread support.
 * -------------------------------------------------------------------
 */
/*
struct thread_context
{
	HANDLE thread;
	bool stop;

	long long total;
	int n;
	unsigned int mask;
	unsigned int l_mask;
	unsigned int r_mask;
	unsigned int t_mask;

	HANDLE ready;
	HANDLE complete;
};

DWORD WINAPI solve_nqueen_proc(LPVOID param)
{
	thread_context* ctx = (thread_context*) param;

	unsigned int mask[32];
	unsigned int l_mask[32];
	unsigned int r_mask[32];
	unsigned int m[32];
	unsigned int t_mask;
	long long total;
	unsigned int index;
	unsigned int mark;

	for(;;) {
		WaitForSingleObject(ctx->ready, INFINITE);
		if(ctx->stop) {
			break;
		}

		int i = 0;

		mask[0] = ctx->mask;
		l_mask[0] = ctx->l_mask;
		r_mask[0] = ctx->r_mask;
		m[0] = mask[0] | l_mask[0] | r_mask[0];
		total = 0;
		t_mask = ctx->t_mask;
		mark = ctx->n;

		while(i >= 0) {
			if((m[i] & t_mask) == t_mask) {
				i--;
			}
			else {
				index = (m[i] + 1) & ~m[i];
				m[i] |= index;
				if((index & t_mask) != 0) {
					if(i + 1 == mark) {
						total++;
						i--;
					}
					else {
						mask[i + 1] = mask[i] | index;
						l_mask[i + 1] = (l_mask[i] | index) << 1;
						r_mask[i + 1] = (r_mask[i] | index) >> 1;
						m[i + 1] = (mask[i + 1] | l_mask[i + 1] | r_mask[i + 1]);
						i++;
					}
				}
				else {
					i --;
				}
			}
		}

		ctx->total = total;

		SetEvent(ctx->complete);
	}

	return 0;
}

long long solve_nqueen_mcpu(int n)
{
	if(n <= 0 || n > 32) {
		return 0;
	}

	SYSTEM_INFO info;
	thread_context* threads;
	int num_threads;

	GetSystemInfo(&info);
	num_threads = info.dwNumberOfProcessors;
	if(num_threads == 1) {
		// only one cpu found, use single thread version
		return solve_nqueen(n);
	}

	threads = new thread_context[num_threads];
	int j;
	for(j = 0; j < num_threads; j++) {
		threads[j].stop = false;
		threads[j].ready = CreateEvent(0, FALSE, FALSE, 0);
		threads[j].complete = CreateEvent(0, FALSE, TRUE, 0);
		threads[j].thread = CreateThread(0, 0, solve_nqueen_proc, threads + j, 0, 0);
		threads[j].total = 0;
	}

	int thread_idx = 0;

	const unsigned int t_mask = (1 << n) - 1;
	long long total = 0;
	unsigned int index;
	
	unsigned int m_mask = 0;
	if(n % 2 == 1) {
		m_mask = 1 << ((n + 1) / 2 - 1);
	}

	for(j = 0; j < (n + 1) / 2; j++) {
		index = 1 << j;
		
		WaitForSingleObject(threads[thread_idx].complete, INFINITE);
		
		if(threads[thread_idx].mask != m_mask) {
			total += threads[thread_idx].total * 2;
		}
		else {
			total += threads[thread_idx].total;
		}
		
		threads[thread_idx].mask = index;
		threads[thread_idx].l_mask = index << 1;
		threads[thread_idx].r_mask = index >> 1;
		threads[thread_idx].t_mask = t_mask;
		threads[thread_idx].total = 0;
		threads[thread_idx].n = n - 1;
		
		SetEvent(threads[thread_idx].ready);
		
		thread_idx = (thread_idx + 1) % num_threads;
	}

	// collect all threads...
	HANDLE* events = new HANDLE[num_threads];
	for(j = 0; j < num_threads; j++) {
		events[j] = threads[j].complete;
	}
	WaitForMultipleObjects(num_threads, events, TRUE, INFINITE);
	for(j = 0; j < num_threads; j++) {
		if(threads[j].mask != m_mask) {
			total += threads[j].total * 2;
		}
		else {
			total += threads[j].total;
		}
		
		threads[j].stop = true;
		SetEvent(threads[j].ready);

		events[j] = threads[j].thread;
	}

	WaitForMultipleObjects(num_threads, events, TRUE, INFINITE);

	for(j = 0; j < num_threads; j++) {
		CloseHandle(threads[j].thread);
		CloseHandle(threads[j].ready);
		CloseHandle(threads[j].complete);
	}
	delete[] threads;
	delete[] events;

	bunk = 3;

	return total;
}
*/


/* --------------------------------------------------------------------------
 * This is a non-recursive version of n-queen backtracking solver for CUDA.
 * It receives multiple initial conditions from a CPU iterator, and count
 * each conditions.
 * --------------------------------------------------------------------------
 */

__global__ void solve_nqueen_cuda_kernel(int n, int mark, unsigned int* total_masks, unsigned int* total_l_masks, unsigned int* total_r_masks, unsigned int* results, int total_conditions)
{
	const int tid = threadIdx.x;
	const int bid = blockIdx.x;
	const int idx = bid * blockDim.x + tid;

	__shared__ unsigned int mask[THREAD_NUM][10];
	__shared__ unsigned int l_mask[THREAD_NUM][10];
	__shared__ unsigned int r_mask[THREAD_NUM][10];
	__shared__ unsigned int m[THREAD_NUM][10];

	__shared__ unsigned int sum[THREAD_NUM];

	const unsigned int t_mask = (1 << n) - 1;
	int total = 0;
	int i = 0;
	unsigned int index;

	if(idx < total_conditions) {
		mask[tid][i] = total_masks[idx];
		l_mask[tid][i] = total_l_masks[idx];
		r_mask[tid][i] = total_r_masks[idx];
		m[tid][i] = mask[tid][i] | l_mask[tid][i] | r_mask[tid][i];

		while(i >= 0) {
			if((m[tid][i] & t_mask) == t_mask) {
				i--;
			}
			else {
				index = (m[tid][i] + 1) & ~m[tid][i];
				m[tid][i] |= index;
				if((index & t_mask) != 0) {
					if(i + 1 == mark) {
						total++;
						i--;
					}
					else {
						mask[tid][i + 1] = mask[tid][i] | index;
						l_mask[tid][i + 1] = (l_mask[tid][i] | index) << 1;
						r_mask[tid][i + 1] = (r_mask[tid][i] | index) >> 1;
						m[tid][i + 1] = (mask[tid][i + 1] | l_mask[tid][i + 1] | r_mask[tid][i + 1]);
						i++;
					}
				}
				else {
					i --;
				}
			}
		}

		sum[tid] = total;
	}
	else {
		sum[tid] = 0;
	}

	__syncthreads();

	// reduction
	if(tid < 64 && tid + 64 < THREAD_NUM) { sum[tid] += sum[tid + 64]; } __syncthreads();
	if(tid < 32) { sum[tid] += sum[tid + 32]; } __syncthreads();
	if(tid < 16) { sum[tid] += sum[tid + 16]; } __syncthreads();
	if(tid < 8) { sum[tid] += sum[tid + 8]; } __syncthreads();
	if(tid < 4) { sum[tid] += sum[tid + 4]; } __syncthreads();
	if(tid < 2) { sum[tid] += sum[tid + 2]; } __syncthreads();
	if(tid < 1) { sum[tid] += sum[tid + 1]; } __syncthreads();

	if(tid == 0) {
		results[bid] = sum[0];
	}
}


long long solve_nqueen_cuda(int n, int steps)
{
	// generating start conditions
	unsigned int mask[32];
	unsigned int l_mask[32];
	unsigned int r_mask[32];
	unsigned int m[32];
	unsigned int index;

	if(n <= 0 || n > 32) {
		return 0;
	}

	unsigned int* total_masks = new unsigned int[steps];
	unsigned int* total_l_masks = new unsigned int[steps];
	unsigned int* total_r_masks = new unsigned int[steps];
	unsigned int* results = new unsigned int[steps];

	unsigned int* masks_cuda;
	unsigned int* l_masks_cuda;
	unsigned int* r_masks_cuda;
	unsigned int* results_cuda;

	cudaMalloc((void**) &masks_cuda, sizeof(int) * steps);
	cudaMalloc((void**) &l_masks_cuda, sizeof(int) * steps);
	cudaMalloc((void**) &r_masks_cuda, sizeof(int) * steps);
	cudaMalloc((void**) &results_cuda, sizeof(int) * steps / THREAD_NUM);

	const unsigned int t_mask = (1 << n) - 1;
	const unsigned int mark = n > 11 ? n - 10 : 2;
	long long total = 0;
	int total_conditions = 0;
	int i = 0, j;

	mask[0] = 0;
	l_mask[0] = 0;
	r_mask[0] = 0;
	m[0] = 0;

	bool computed = false;

	for(j = 0; j < n / 2; j++) {
		index = (1 << j);
		m[0] |= index;
		
		mask[1] = index;
		l_mask[1] = index << 1;
		r_mask[1] = index >> 1;
		m[1] = (mask[1] | l_mask[1] | r_mask[1]);
		i = 1;
			
		while(i > 0) {
			if((m[i] & t_mask) == t_mask) {
				i--;
			}
			else {
				index = (m[i] + 1) & ~m[i];
				m[i] |= index;
				if((index & t_mask) != 0) {
					mask[i + 1] = mask[i] | index;
					l_mask[i + 1] = (l_mask[i] | index) << 1;
					r_mask[i + 1] = (r_mask[i] | index) >> 1;
					m[i + 1] = (mask[i + 1] | l_mask[i + 1] | r_mask[i + 1]);
					i++;
					if(i == mark) {
						total_masks[total_conditions] = mask[i];
						total_l_masks[total_conditions] = l_mask[i];
						total_r_masks[total_conditions] = r_mask[i];
						total_conditions++;
						if(total_conditions == steps) {
							if(computed) {
								cudaMemcpy(results, results_cuda, sizeof(int) * steps / THREAD_NUM, cudaMemcpyDeviceToHost);

								for(int j = 0; j < steps / THREAD_NUM; j++) {
									total += results[j];
								}

								computed = false;
							}

							// start computation
							cudaMemcpy(masks_cuda, total_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);
							cudaMemcpy(l_masks_cuda, total_l_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);
							cudaMemcpy(r_masks_cuda, total_r_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);

							solve_nqueen_cuda_kernel<<<steps/THREAD_NUM, THREAD_NUM>>>(n, n - mark, masks_cuda, l_masks_cuda, r_masks_cuda, results_cuda, total_conditions);

							computed = true;

							total_conditions = 0;
						}
						i--;
					}
				}
				else {
					i --;
				}
			}
		}
	}
	

	if(computed) {
		cudaMemcpy(results, results_cuda, sizeof(int) * steps / THREAD_NUM, cudaMemcpyDeviceToHost);

		for(int j = 0; j < steps / THREAD_NUM; j++) {
			total += results[j];
		}

		computed = false;
	}

	cudaMemcpy(masks_cuda, total_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);
	cudaMemcpy(l_masks_cuda, total_l_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);
	cudaMemcpy(r_masks_cuda, total_r_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);

	solve_nqueen_cuda_kernel<<<steps/THREAD_NUM, THREAD_NUM>>>(n, n - mark, masks_cuda, l_masks_cuda, r_masks_cuda, results_cuda, total_conditions);

	cudaMemcpy(results, results_cuda, sizeof(int) * steps / THREAD_NUM, cudaMemcpyDeviceToHost);

	for(int j = 0; j < steps / THREAD_NUM; j++) {
		total += results[j];
	}	
	
	total *= 2;
	
	if(n % 2 == 1) {
		computed = false;
		total_conditions = 0;

		index = (1 << (n - 1) / 2);
		m[0] |= index;
		
		mask[1] = index;
		l_mask[1] = index << 1;
		r_mask[1] = index >> 1;
		m[1] = (mask[1] | l_mask[1] | r_mask[1]);
		i = 1;
			
		while(i > 0) {
			if((m[i] & t_mask) == t_mask) {
				i--;
			}
			else {
				index = (m[i] + 1) & ~m[i];
				m[i] |= index;
				if((index & t_mask) != 0) {
					mask[i + 1] = mask[i] | index;
					l_mask[i + 1] = (l_mask[i] | index) << 1;
					r_mask[i + 1] = (r_mask[i] | index) >> 1;
					m[i + 1] = (mask[i + 1] | l_mask[i + 1] | r_mask[i + 1]);
					i++;
					if(i == mark) {
						total_masks[total_conditions] = mask[i];
						total_l_masks[total_conditions] = l_mask[i];
						total_r_masks[total_conditions] = r_mask[i];
						total_conditions++;
						if(total_conditions == steps) {
							if(computed) {
								cudaMemcpy(results, results_cuda, sizeof(int) * steps / THREAD_NUM, cudaMemcpyDeviceToHost);

								for(int j = 0; j < steps / THREAD_NUM; j++) {
									total += results[j];
								}

								computed = false;
							}

							// start computation
							cudaMemcpy(masks_cuda, total_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);
							cudaMemcpy(l_masks_cuda, total_l_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);
							cudaMemcpy(r_masks_cuda, total_r_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);

							solve_nqueen_cuda_kernel<<<steps/THREAD_NUM, THREAD_NUM>>>(n, n - mark, masks_cuda, l_masks_cuda, r_masks_cuda, results_cuda, total_conditions);

							computed = true;

							total_conditions = 0;
						}
						i--;
					}
				}
				else {
					i --;
				}
			}
		}

		if(computed) {
			cudaMemcpy(results, results_cuda, sizeof(int) * steps / THREAD_NUM, cudaMemcpyDeviceToHost);

			for(int j = 0; j < steps / THREAD_NUM; j++) {
				total += results[j];
			}

			computed = false;
		}

		cudaMemcpy(masks_cuda, total_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);
		cudaMemcpy(l_masks_cuda, total_l_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);
		cudaMemcpy(r_masks_cuda, total_r_masks, sizeof(int) * total_conditions, cudaMemcpyHostToDevice);

		solve_nqueen_cuda_kernel<<<steps/THREAD_NUM, THREAD_NUM>>>(n, n - mark, masks_cuda, l_masks_cuda, r_masks_cuda, results_cuda, total_conditions);

		cudaMemcpy(results, results_cuda, sizeof(int) * steps / THREAD_NUM, cudaMemcpyDeviceToHost);

		for(int j = 0; j < steps / THREAD_NUM; j++) {
			total += results[j];
		}
	}

	cudaFree(masks_cuda);
	cudaFree(l_masks_cuda);
	cudaFree(r_masks_cuda);
	cudaFree(results_cuda);

	delete[] total_masks;
	delete[] total_l_masks;
	delete[] total_r_masks;
	delete[] results;

	bunk = 1;

	return total;
}


bool InitCUDA()
{
    int count;

    cudaGetDeviceCount(&count);
    if(count == 0) {
        fprintf(stderr, "There is no device.\n");
        return false;
    }

    int i;
    for(i = 0; i < count; i++) {
        cudaDeviceProp prop;
        if(cudaGetDeviceProperties(&prop, i) == cudaSuccess) {
            if(prop.major >= 1) {
                break;
            }
        }
    }

    if(i == count) {
        fprintf(stderr, "There is no device supporting CUDA 1.x.\n");
        return false;
    }

    cudaSetDevice(i);

    return true;
}


int main(int argc, char** argv)
{
  unsigned int hTimer;
  double  gpuTime;
  // initialise card and timer
  int deviceCount;                                                         
  CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceCount(&deviceCount));                
  if (deviceCount == 0) {                                                  
      fprintf(stderr, "There is no device.\n");                            
      exit(EXIT_FAILURE);                                                  
  }                                                                        
  int dev;                                                                 
  for (dev = 0; dev < deviceCount; ++dev) {                                
      cudaDeviceProp deviceProp;                                           
      CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceProperties(&deviceProp, dev));   
      if (deviceProp.major >= 1)                                           
          break;                                                           
  }                                                                        
  if (dev == deviceCount) {                                                
      fprintf(stderr, "There is no device supporting CUDA.\n");            
      exit(EXIT_FAILURE);                                                  
  }                                                                        
  else                                                                     
      CUDA_SAFE_CALL(cudaSetDevice(dev));  
  CUT_SAFE_CALL( cutCreateTimer(&hTimer) );

	int n = 8;
	clock_t start, end;
	long long solution;
	bool cpu = true, gpu = true;
	int argstart = 1, steps = 24576;

	if(argc >= 2 && argv[1][0] == '-') {
		if(argv[1][1] == 'c' || argv[1][1] == 'C') {
			gpu = false;
		}
		else if(argv[1][1] == 'g' || argv[1][1] == 'G') {
			cpu = false;
		}

		argstart = 2;
	}

	if(argc < argstart + 1) {
		printf("Usage: %s [-c|-g] n steps\n", argv[0]);
		printf("  -c: CPU only\n");
		printf("  -g: GPU only\n");
		printf("  n: n-queen\n");
		printf("  steps: step for GPU\n");
		printf("Default to 8 queen\n");
	}
	else {
		n = atoi(argv[argstart]);
		if(n <= 1 || n > 32) {
			printf("Invalid n, n should be > 1 and <= 32\n");
			printf("Note: n > 18 will require a very very long time to compute!\n");
			return 0;
		}

		if(argc >= argstart + 2) {
			steps = atoi(argv[argstart + 1]);
			if(steps <= THREAD_NUM || steps % THREAD_NUM != 0) {
				printf("Invalid step, step should be multiple of %d\n", THREAD_NUM);
				return 0;
			}
		}
	}

	if(gpu) {
	    if(!InitCUDA()) {
		    return 0;
		}

		printf("CUDA initialized.\n");
	}

	if(cpu) {
  CUDA_SAFE_CALL( cudaThreadSynchronize() );
  CUT_SAFE_CALL( cutResetTimer(hTimer) );
  CUT_SAFE_CALL( cutStartTimer(hTimer) );

		//start = clock();
		solution = solve_nqueen(n); //solve_nqueen_mcpu(n);
		//solution = solve_nqueen(n);
		//end = clock();
  CUT_SAFE_CALL( cutStopTimer(hTimer) );
  gpuTime = cutGetTimerValue(hTimer);

		printf("CPU: %d queen = %lld  time = %f msec\n", n, solution, gpuTime);
	}

	if(gpu) {
		//start = clock();
  CUDA_SAFE_CALL( cudaThreadSynchronize() );
  CUT_SAFE_CALL( cutResetTimer(hTimer) );
  CUT_SAFE_CALL( cutStartTimer(hTimer) );
		solution = solve_nqueen_cuda(n, steps);
		//end = clock();
  CUT_SAFE_CALL( cutStopTimer(hTimer) );
  gpuTime = cutGetTimerValue(hTimer);
		printf("GPU: %d queen = %lld  time = %f msec\n", n, solution, gpuTime);
	}

	return 0;
}