public static void Kernel(double[] positionsx, double[] positionsy, double[] velocitiesx, double[] velocitiesy, double[] accelerationsx, double[] accelerationsy, int width, int height, float mousex, float mousey, int[,] squareFish, int[] squaresStart, int[] fishInSquare, int squaresInRow, int squaresNumber, int[] bitmap) { int ind = blockIdx.x * blockDim.x + threadIdx.x; FishFunctions.AlignWithOtherFish(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, squareFish, squaresStart, fishInSquare, squaresInRow, squaresNumber); FishFunctions.CohesionWithOtherFish(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, squaresStart, squareFish, fishInSquare, squaresNumber, squaresInRow); FishFunctions.AvoidOtherFish(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, squaresStart, squareFish, fishInSquare, squaresInRow, squaresNumber); if (mousex >= 0 && mousey >= 0) { FishFunctions.AvoidMouse(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, mousex, mousey); } FishFunctions.UpdateFish(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, width, height, mousex, mousey); FishFunctions.Edges(positionsx, positionsy, ind, width, height); DeviceFunction.SyncThreads(); int col = (255 << 24) + (0 << 16) + (255 << 8) + 255; int x = (int)positionsx[ind]; int y = (int)positionsy[ind]; CircleDrawing.CircleBresenham(x, y, 2, bitmap, width, height, col); }
private void RunCpu() { for (int i = 0; i < fishNumber; i++) { FishCPU.KernelCpu(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, windowWidth, windowHeight, Mouse.X, Mouse.Y, squarefish, squarestart, fishinsquere, squaresInRow, squaresNumber, i, bitmap); } FishFunctions.UpdateSquares(squarefish, squarestart, fishinsquere, positionsx, positionsy, fishNumber, squaresNumber, squareSize, windowWidth); pictureBox1.Invalidate(); }
private void OnFormLoad(object sender, EventArgs e) { random = new Random(); pictureBox1.SizeMode = PictureBoxSizeMode.Normal; windowWidth = pictureBox1.Width; windowHeight = pictureBox1.Height; bitmap = new int[windowWidth * windowHeight]; squaresNumber = (int)Math.Ceiling((double)windowHeight / squareSize) * (int)Math.Ceiling((double)windowWidth / squareSize); squaresInRow = (windowWidth / squareSize); positionsx = new double[fishNumber]; positionsy = new double[fishNumber]; velocitiesx = new double[fishNumber]; velocitiesy = new double[fishNumber]; accelerationsx = new double[fishNumber]; accelerationsy = new double[fishNumber]; fishinsquere = new int[fishNumber]; squarestart = new int[squaresNumber]; squarefish = new int[fishNumber, 2]; for (int i = 0; i < fishNumber; i++) { var r1 = random.Next(-10, 10); var r2 = random.Next(-10, 10); var Velocity = new Vector2(r1, r2); if (Velocity.Length() < 2 || Velocity.Length() > 4) { float maxValue = (float)random.NextDouble() % 3 + 2; float vLength = Velocity.Length(); if (vLength > 0) { Velocity *= new Vector2(maxValue / vLength, maxValue / vLength); } } positionsx[i] = random.Next(1, pictureBox1.Width - 1); positionsy[i] = random.Next(1, pictureBox1.Height - 1); velocitiesx[i] = Velocity.X; velocitiesy[i] = Velocity.Y; accelerationsx[i] = 0; accelerationsy[i] = 0; } FishFunctions.UpdateSquares(squarefish, squarestart, fishinsquere, positionsx, positionsy, fishNumber, squaresNumber, squareSize, windowWidth); Timer timer = new Timer(); timer.Interval = 10; timer.Tick += Timer_Tick; timer.Start(); }
private void RunGpu() { var gpu = Alea.Gpu.Default; gpu.Launch(FishGPU.Kernel, new Alea.LaunchParam((int)Math.Ceiling((double)fishNumber / 512), 512), positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, windowWidth, windowHeight, Mouse.X, Mouse.Y, squarefish, squarestart, fishinsquere, squaresInRow, squaresNumber, bitmap); FishFunctions.UpdateSquares(squarefish, squarestart, fishinsquere, positionsx, positionsy, fishNumber, squaresNumber, squareSize, windowWidth); if (!drawOnGpu) { pictureBox1.Invalidate(); } else { DrawFish(); } }