 		exit(-1); // fail
 	}
 
-	int i, j, magnitude, sum1, sum2; 
 	int *out = result;
 
 	// Set initial values of result
 	for (int col=0; col<ysize; col++) {
 		for (int row=0; row<xsize; row++) { 
-			*out++ = 0; 
-		}
-	}
-
-	for (i = 1;  i < ysize - 1; i++) {
-		for (j = 1; j < xsize -1; j++) {
-			int offset = i*xsize + j;
-
-			sum1 =  pic[xsize * (i - 1) + j + 1 ] - pic[xsize * (i - 1) + j - 1] +
-					2 * pic[xsize * (i) + j + 1 ] - 2 * pic[xsize * (i) + j - 1] +
-					pic[xsize * (i + 1) + j + 1 ] - pic[xsize * (i + 1) + j - 1];
-
-			sum2 = pic[xsize * (i - 1) + j - 1] + 2 * pic[xsize * (i - 1) + j] +
-				   pic[xsize * (i - 1) + j + 1] - pic[xsize * (i + 1) + j - 1] - 
-				   2 * pic[xsize * (i + 1) + j] - pic[xsize * (i + 1) + j + 1];
-
-			magnitude =  sum1 * sum1 + sum2 * sum2;
-
-			if (magnitude > thresh) {
-				result[offset] = 255;
-			}
-			else { 
-				result[offset] = 0;
-			}
-		}
-	}
-
+			//*out++ = 0;
+			*out++ = -1; 			
+		}
+	}
+
+	int *imageWidth, *imageHeight, *image, *output, *threshold;
+
+	cudaMalloc((void **)&imageWidth, sizeof(int));
+	cudaMalloc((void **)&imageHeight, sizeof(int));
+	cudaMalloc((void **)&image, numbytes);
+	cudaMalloc((void **)&output, numbytes);
+	cudaMalloc((void **)&threshold, sizeof(int));
+
+	cudaMemcpy(imageWidth, &xsize, sizeof(int), cudaMemcpyHostToDevice);
+	cudaMemcpy(imageHeight, &ysize, sizeof(int), cudaMemcpyHostToDevice);
+	cudaMemcpy(image, pic, numbytes, cudaMemcpyHostToDevice);
+	cudaMemcpy(output, result, numbytes, cudaMemcpyHostToDevice);
+	cudaMemcpy(threshold, &thresh, sizeof(int), cudaMemcpyHostToDevice);
+	
+	sobel<<<1,1>>>(imageWidth, imageHeight, image, output, threshold);
+
+	cudaMemcpy(result, output, numbytes, cudaMemcpyDeviceToHost);
+
+	cudaFree(imageWidth);
+	cudaFree(imageHeight);
+	cudaFree(image);
+	cudaFree(output);
+	cudaFree(threshold);
+		
 	write_ppm( "result.ppm", xsize, ysize, 255, result);
 	fprintf(stderr, "sobel done\n"); 
 }

	}

	fclose(fp);
}

int main( int argc, char **argv )

	int xsize, ysize, maxval;
	unsigned int *pic = read_ppm( filename, &xsize, &ysize, &maxval ); 

	int numbytes =  xsize * ysize * 3 * sizeof( int );
	int *result = (int *) malloc( numbytes );

