# Julia experiment in Chapter 4 of "GPU High Performance programming Cuda practice Chinese"

Source: Internet
Author: User

A variety of problems occurred throughout the process. First, I packed the project that I debugged and provided the download.

` 1 /* 2 * Copyright 1993-2010 NVIDIA Corporation.  All rights reserved. 3 * 4 * NVIDIA Corporation and its licensors retain all intellectual property and 5 * proprietary rights in and to this software and related documentation. 6 * Any use, reproduction, disclosure, or distribution of this software 7 * and related documentation without an express license agreement from 8 * NVIDIA Corporation is strictly prohibited. 9 *10 * Please refer to the applicable NVIDIA end user license agreement (EULA)11 * associated with this source code for terms and conditions that govern12 * your use of this NVIDIA software.13 *14 */15 16 #include <GL\glut.h>17 #include "cuda.h"18 #include "cuda_runtime.h"19 #include "device_launch_parameters.h"20 #include "../common/book.h"21 #include "../common/cpu_bitmap.h"22 23 #define DIM 100024 25 struct cuComplex {26     float   r;27     float   i;28     __device__ cuComplex(float a, float b) : r(a), i(b)  {}29     __device__ float magnitude2(void) {30         return r * r + i * i;31     }32     __device__ cuComplex operator*(const cuComplex& a) {33         return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);34     }35     __device__ cuComplex operator+(const cuComplex& a) {36         return cuComplex(r + a.r, i + a.i);37     }38 };39 40 __device__ int julia(int x, int y) {41     const float scale = 1.5;42     float jx = scale * (float)(DIM / 2 - x) / (DIM / 2);43     float jy = scale * (float)(DIM / 2 - y) / (DIM / 2);44 45     cuComplex c(-0.8, 0.156);46     cuComplex a(jx, jy);47 48     int i = 0;49     for (i = 0; i<200; i++) {50         a = a * a + c;51         if (a.magnitude2() > 1000)52             return 0;53     }54 55     return 1;56 }57 58 __global__ void kernel(unsigned char *ptr) {59     // map from blockIdx to pixel position60     int x = blockIdx.x;61     int y = blockIdx.y;62     int offset = x + y * gridDim.x;63 64     // now calculate the value at that position65     int juliaValue = julia(x, y);66     ptr[offset * 4 + 0] = 255 * juliaValue;67     ptr[offset * 4 + 1] = 0;68     ptr[offset * 4 + 2] = 0;69     ptr[offset * 4 + 3] = 255;70 }71 72 // globals needed by the update routine73 struct DataBlock {74     unsigned char   *dev_bitmap;75 };76 77 int main(void) {78     DataBlock   data;79     CPUBitmap bitmap(DIM, DIM, &data);80     unsigned char    *dev_bitmap;81 82     HANDLE_ERROR(cudaMalloc((void**)&dev_bitmap, bitmap.image_size()));83     data.dev_bitmap = dev_bitmap;84 85     dim3    grid(DIM, DIM);86     kernel << <grid, 1 >> >(dev_bitmap);87 88     HANDLE_ERROR(cudaMemcpy(bitmap.get_ptr(), dev_bitmap,89         bitmap.image_size(),90         cudaMemcpyDeviceToHost));91 92     HANDLE_ERROR(cudaFree(dev_bitmap));93 94     bitmap.display_and_exit();95 }`

Problems:

Question 1

`calling a host function("cuComplex::cuComplex") from a __device__/__global__ function("julia") is not allowedcalling a host function("cuComplex::cuComplex") from a __device__/__global__ function("julia") is not allowedcalling a host function("cuComplex::cuComplex") from a __device__/__global__ function("cuComplex::operator *") is not allowedcalling a host function("cuComplex::cuComplex") from a __device__/__global__ function("cuComplex::operator +") is not allowed`

This is because there is a problem with the Code provided in the original work. The code in the structure in the original work is

`cuComplex(float a, float b) : r(a), i(b)  {}`

Modify it as follows:

` __device__ cuComplex(float a, float b) : r(a), i(b)  {}`

Question 2

Error lnk2019: an external symbol that cannot be parsed [email protected]. This symbol is referenced in the function [email protected]. 1> gears. OBJ: Error lnk2019: an external symbol that cannot be parsed ___ GL

This is because my OpenGL file is not

`#include <GL\glut.h>`

The glut. h file must be in the following path.

`C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\GL`

If the GL Folder does not exist, you need to create it manually. The structure is shown in:

Note:

To run the sample code, you need to extract the executable part. To reduce the trouble of manual modification, you also need to pay attention to the hierarchical relationships of various file directories. My example is as follows:

The following figure shows the result of the painstaking effort:

It's really nice. Like one!

Julia experiment in Chapter 4 of "GPU High Performance programming Cuda practice Chinese"

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.