I'm trying to print a chessboard to the console using a 2D array. For testing purposes, I'm trying to simply populate the board as 'x' chars. However, I keep getting an ArrayIndexOutOfBounds exception when I try to populate the array with the following:
public void setupBoard(){
for (int i=0; i < height; i++){
for (int j=0; j < width; j++){
boardArray[i][j] = 'x';
}
}
}
The error apparently occurs at boardArray[i][j] = 'x';
Everything seems to be in order, I dont see why this isnt working.
EDIT: I got the array to populate, but now I cannot format the printing correctly. The contents all print on one line, instead of as an 8x8 square of 'x' chars. Here's what I have now:
public void displayBoard(){
for (int k=0; k < boardArray.length; k++)
for (int l=0; l < boardArray[k].length; l++){
System.out.print(boardArray[k][l]);
}
System.out.println();
}
Well, presumably it's because you haven't created the board properly. Unfortunately you haven't shown us the array creation statement. It should be something like:
char[][] boardArray = new char[height][width];
EDIT: Okay, now for the printing part. Your current code is:
public void displayBoard(){
for (int k=0; k < boardArray.length; k++)
for (int l=0; l < boardArray[k].length; l++){
System.out.print(boardArray[k][l]);
}
System.out.println();
}
This is equivalent to:
public void displayBoard() {
for (int k = 0; k < boardArray.length; k++) {
for (int l = 0; l < boardArray[k].length; l++) {
System.out.print(boardArray[k][l]);
}
}
System.out.println();
}
... so you're only calling println after the outer loop has finished. If you just move the call to println to after the inner loop, it'll be fine:
public void displayBoard() {
for (int k = 0; k < boardArray.length; k++) {
for (int l = 0; l < boardArray[k].length; l++) {
System.out.print(boardArray[k][l]);
}
System.out.println();
}
}
Note that this sort of thing is clearer if you always include braces for loops, if statements etc.
Related
I am trying to calculate the euclidean distance for KNN but in parallel using dpc++. the training dataset contains 5 features and 1600 rows, while I want to calculate the distance between the current test point and each training point on the grid in parallel, but I keep getting an error regarding sycl kernal.
code for the function:
code
std::vector<double> distance_calculation_FPGA(queue& q,const std::vector<std::vector<double>>& dataset,const std::vector<double>& curr_test) {
range<1> num_items{ dataset.size()};
std::vector<double>res;
res.resize(dataset.size());
buffer dataset_buf(dataset);
buffer curr_test_buf(curr_test);
buffer res_buf(res.data(), num_items);
q.submit([&](handler& h) {
accessor a(dataset_buf, h, read_only);
accessor b(curr_test_buf, h, read_only);
accessor dif(res_buf, h, write_only, no_init);
h.parallel_for(num_items, [=](auto i) {
for (int j = 0; j <(const int) a[i].size(); ++j) {
dif[i] += (a[i][j] - b[j]) * (a[i][j] - b[j]) ;
}
});
});
for (int i = 0; i < res.size(); ++i) {
std::cout << res[i] << std::endl;
}
//old distance calculation (serial)
//for (int i = 0; i < dataset.size(); ++i) {
// double dis = 0;
// for (int j = 0; j < dataset[i].size(); ++j) {
// dis += (curr_test[j] - dataset[i][j]) * (curr_test[j] - dataset[i][j]);
//}
//res.push_back(dis);
//}
return res;
}
the error I am receiving:
SYCL kernel cannot call a variadic function
SYCL kernel cannot call an undefined function without SYCL_EXTERNAL attribute
Would be extremely grateful for any help!
Thanks
We tried running your code by creating dummy 'dataset' and 'curr_test' variables. We were able to run the program successfully. Please refer this thread
Please refer to the complete code attached below.
#include <CL/sycl.hpp>
#include <iostream>
using namespace sycl;
std::vector<double> distance_calculation_FPGA(queue& q,const std::vector<std::vector<double>>& dataset,const std::vector<double>& curr_test)
{
range<1> num_items{ dataset.size()};
std::vector<double>res;
res.resize(dataset.size());
buffer dataset_buf(dataset);
buffer curr_test_buf(curr_test);
buffer res_buf(res.data(), num_items);
q.submit([&](handler& h) {
accessor a(dataset_buf, h, read_only);
accessor b(curr_test_buf, h, read_only);
accessor dif(res_buf, h, write_only, no_init);
h.parallel_for(num_items, [=](auto i) {
for (int j = 0; j <(const int) a[i].size(); ++j) {
// dif[i] += (a[i][j] - b[j]) * (a[i][j] - b[j]) ;
dif[i]+=a[i][j];
}
});
});
q.wait(); //We have added this line of code for synchronization.
for (int i : res) {
std::cout <<i<< std::endl;
}
return res;
}
int main(){
std::vector<std::vector<double>> dataset;
for(int i=0;i<5;i++)
{
std::vector<double> d;
for(int j=0;j<1600;j++)
{
d.push_back((double)j);
}
dataset.push_back(d);
}
std::vector<double> curr_test;
for(int i=0;i<1600;i++)
{
curr_test.push_back((double)i);
}
queue q;
std::cout << "Running on "<<
q.get_device().get_info<sycl::info::device::name>()<< std::endl;
//print the device name as a test to check the parallelisation
distance_calculation_FPGA(q,dataset,curr_test);
return 0;
}
For some reason the element b[m][n] is a random number instead of being 1. The same thing happens if I try to printf a[n][m]. Please I need some help on this!
#include <stdio.h>
#include <stdlib.h>
void **alloc(int n,int m)
{
int **x=(int**)malloc(n*sizeof(int*));
for(int i=1; i<=n; i++)
x[i] =(int*)malloc(m*sizeof(int*));
return x ;
}
void read(int **a,int n,int m,FILE *f)
{
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
fscanf(f,"%d",&a[i][j]);
}
void print(int **a,int n,int m)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void transpose(int **a,int n,int m,int ***b)
{
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
b[j][i]=a[i][j];
}
int main()
{
int n,m;
FILE *f;
f=fopen("in.txt","r");
if(f==NULL)
printf("Error");
fscanf(f,"%d %d",&n,&m);
int **a=alloc(n,m);
read(a,n,m,f);
int **b=alloc(m,n);
transpose(a,n,m,b);
print(b,m,n);
return 0;
}
This is the input and output, everything works except the b[m][n] element
There are several issues:
If this is C code, casting the result of malloc is at best pointless and at worst dangerous, as it might hide bugs. (This does not apply for C++ code.)
Your "inner" malloc uses sizeof(int*), when it should be using sizeof(int).
Your alloc function returns void**, which is not wrong as such, but why not let it return int** instead?
This is the most important one: C array indexing starts at zero, so if you have an array of N elements, they are numbered 0 though N-1.
I am trying to build a memory game with 16 pairs of cards.
I do not know exactly how to implement an OnClick event. I am new at using C++Builder, so please help.
The images are in an array, I allocate them dynamically like this:
for(int i=0;i<4;i++)
{
for(int j = 0; j < 8 ; j++)
{
VectorOfImages[i*8+j]=new Card(9+i*112,9+j*112,pan, 0);
VectorOfImages[i*8+j]->image->Picture>LoadFromFile("...OOP\\c\\images\\0.bmp");
VectorOfImages[i*8+j]->image->Tag=i*8+j;
VectorOfImages[i*8+j]->image->Enabled=false;
}
}
OnClick is a property of TImage, you can assign it like you would any other property, eg:
for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 8; ++j)
{
int idx = (i*8) + j;
VectorOfImages[idx] = new Card(9+i*112, 9+j*112, pan, 0);
VectorOfImages[idx]->image->Picture->LoadFromFile("...OOP\\c\\images\\0.bmp");
VectorOfImages[idx]->image->Tag = idx;
VectorOfImages[idx]->image->Enabled = false;
VectorOfImages[idx]->OnClick = &ImageClicked; // <-- here
}
}
Then, add ImageClicked() to your Form:
private:
void __fastcall ImageClicked(TObject *Sender);
...
void __fastcall TMyForm::ImageClicked(TObject *Sender)
{
// Sender points at the TImage that was clicked...
TImage *Image = static_cast<TImage*>(Sender);
// use Image as needed...
}
I am trying to create a counter that increases if an Image contained in an ImageView array is set to null. I have a Bullet class that calls the method below
Whenever I kill an enemy it sets the image to 'null'
public void Collision(ImageView ene[], Rectangle b){
for (int i = 0; i < 10 * wave; i++) {
if (ene[i].getBoundsInParent().intersects(b.getBoundsInParent())){
ene[i].setVisible(false);
ene[i].setImage(null);
score();
bulletGone();
}
}
}
My problem is that it prints out the first "Null count: " in the checkWave method, but never reaches the for loop. Am I comparing to null incorrectly?
Look at this checkWave(ImageView e[])
now check your Collision method pal, who do you set to null? go back to your checkWave(ImageView e[]) method, who you check for null?
hope you find your solution.
one tip: int counter = 10 * wave;//its final
for (int i = 0; i < 10 * wave; i++) {//save yourself some keys
or (int i = 0; i < counter; i++) {
EDIT
for (int i = 0; i < counter; i++) {
if (e[i].getImage() == null){ //now your loop will do
nullCount++;
System.out.println("Null count: " + nullCount);
}
}
I have written a simple program that does autocorrelation as follows...I've used pgi accelerator directives to move the computation to GPUs.
//autocorrelation
void autocorr(float *restrict A, float *restrict C, int N)
{
int i, j;
float sum;
#pragma acc region
{
for (i = 0; i < N; i++) {
sum = 0.0;
for (j = 0; j < N; j++) {
if ((i+j) < N)
sum += A[j] * A[i+j];
else
continue;
}
C[i] = sum;
}
}
}
I wrote a similar program in OpenCL, but I am not getting correct results. The program is as follows...I am new to GPU programming, so apart from hints that could fix my error, any other advices are welcome.
__kernel void autocorrel1D(__global double *Vol_IN, __global double *Vol_AUTOCORR, int size)
{
int j, gid = get_global_id(0);
double sum = 0.0;
for (j = 0; j < size; j++) {
if ((gid+j) < size)
{
sum += Vol_IN[j] * Vol_IN[gid+j];
}
else
continue;
}
barrier(CLK_GLOBAL_MEM_FENCE);
Vol_AUTOCORR[gid] = sum;
}
Since I have passed the dimension to be 1, so I am considering my get_global_size(0) call would give me the id of the current block, which is used to access the input 1d array.
Thanks,
Sayan
The code is correct. As far as I know, that should run fine and give corret results.
barrier(CLK_GLOBAL_MEM_FENCE); is not needed. You'll get more speed without that sentence.
Your problem should be outside the kernel, check that you a re passing correctly the input, and you are taking out of GPU the correct data.
BTW, I supose you are using a double precision suported GPU as you are doing double calcs.
Check that you are passing also double values. Remember you CAN't point a float pointer to a double value, and viceversa. That will give you wrong results.