|-
|
__global__ void superSolve(int * d_a) { __shared__ bool rowHas[N][N]; __shared__ bool colHas[N][N]; __shared__ bool boxHas[N][N]; __shared__ int added, past; __shared__ int rowCount[N][N]; __shared__ int colCount[N][N]; __shared__ int boxCount[N][N]; int row = threadIdx.x; int col = threadIdx.y; int box = row / BOXWIDTH + (col / BOXWIDTH) * BOXWIDTH; int offset = col + (row % BOXWIDTH) * BOXWIDTH + (box % BOXWIDTH); int gridIdx = col * N + row;
int at = d_a __shared__ bool rowHas[N][N]; __shared__ bool colHas[N][N]; __shared__ bool boxHas[N][gridIdxN]; __shared__ int added, past;
if (!gridIdx) { //only 0 needs to set changedNumber of spaces which can place the number added = -1 __shared__ int rowCount[N][N]; past = -2; } rowHas __shared__ int colCount[colN][rowN] = false; colHas __shared__ int boxCount[colN][N]; int row] = falsethreadIdx.x; boxHas[ int col][row] = falsethreadIdx.y; __syncthreads int box = row / BOXWIDTH + (col / BOXWIDTH)* BOXWIDTH;
if (at != UNASSIGNED) { //Unique identifier for each square in row, col, box rowHas[row][at - 1] int offset = true; colHas[col][at - 1] = true; boxHas[+ (row % BOXWIDTH) * BOXWIDTH + (box][at - 1] = true% BOXWIDTH); }
//Squares location in the grid int gridIdx = col * N + row; int at = d_a[gridIdx]; if (!gridIdx) { //Thread at 0,0 sets values added = -1; past = -2; } rowHas[col][row] = false; colHas[col][row] = false; boxHas[col][row] = false; __syncthreads(); if (at != UNASSIGNED) { rowHas[row][at - 1] = true; colHas[col][at - 1] = true; boxHas[box][at - 1] = true; } //Previous loop has not changed any values while (added != past) { //RESET counters rowCount[col][row] = 0; colCount[col][row] = 0; boxCount[col][row] = 0; __syncthreads(); if (!gridIdx)//need to change a new value past = added; int count = 0; //number of values which can fit in this square int guess = at;//last value found which can fit in this square for (int idx = 0; idx < N; ++idx) { int num = (idx + offset) % N; if (at == UNASSIGNED && !(rowHas[row][num] || colHas[col][num] || boxHas[box][num])) { count++; guess = num + 1; rowCount[row][num] ++; colCount[col][num] ++; boxCount[box][num] ++; } __syncthreads(); } //Only ONE value can fit in this spot if (count == 1) { at = guess--; d_a[gridIdx] = at; rowHas[row][guess] = true; colHas[col][guess] = true; boxHas[box][guess] = true; added = gridIdx; } __syncthreads(); if (at == UNASSIGNED) { for (int idx = 0; idx < N; ++idx) { if (!(rowHas[row][idx] || colHas[col][idx] || boxHas[box][idx]) && (boxCount[box][idx] == 1 || rowCount[row][idx] == 1 || colCount[col][idx] == 1)) { at = idx + 1; d_a[gridIdx] = at; rowHas[row][idx] = true; colHas[col][idx] = true; boxHas[box][idx] = true; added = gridIdx; } } } __syncthreads(); } }
|}
=== Assignment 3 ===