Wednesday, April 26, 2017

Visual Studio 2015 Boost 1.62.0 빌드

call "%VS140COMNTOOLS%..\..\VC\vcvarsall.bat" x86

set cores=%NUMBER_OF_PROCESSORS%
echo Building boost with %cores% cores

rem cd boost_1_62_0
call bootstrap.bat

rem Most libraries can be static libs
b2 -j%cores% toolset=msvc-14.0 address-model=64 architecture=x86 link=static threading=multi runtime-link=shared --build-type=minimal stage --stagedir=stage/x64
b2 -j%cores% toolset=msvc-14.0 address-model=32 architecture=x86 link=static threading=multi runtime-link=shared --build-type=minimal stage --stagedir=stage/win32

pause

Tuesday, April 25, 2017

Linux BSP(Board Support Package)의 정의

BSP(Board Support Package)란 이름의 의미 그대로 Board를 사용할 수 있도록 하기 위한 Software 묶음이라고 보면 될 것이다. 자세히 설명하자면 운영체제를 로드하기 위한 최소한의 장치를 지원하고, 하드웨어 보드의 모든 장치를 위한 드라이버를 말한다. 몇몇의 공급자들은 루트 파일 시스템, 임베디드 시스템에서 실행하는 프로그램을 만들기 위한 툴체인 그리고 장치들을 위한 configurator를 제공한다.

BSP의 구성은 일반적으로 아래와 같다.

1. Bootloader
시스템의 하드웨어를 초기화하고 운영체제의 이미지를 RAM에 올려주는 역할을 하는 시스템 프로그램으로 하드웨어 의존성이 강하다.

2. OAL(OEM Adaption Layer)
HAL(Hardware Abstraction Layer) 아래에 위치하며, 하드웨어 초기화 및 관리를 지원한다.
HAL란 컴퓨터의 하드웨어와 소프트웨어 사이의 추상화 계층이다.

3. Device Driver
특정 하드웨어나 장치를 제어하기 위한 커널의 일부분으로 동작하는 프로그램으로, 각각 장치 드라이버가 프로그램 되어 커널에 통합되어 실행된다.

iOS 오픈소스 라이브러리 모음

SDWebImage - 이미지 다운로더 및 캐쉬 라이브러리
https://github.com/rs/SDWebImage

Toast - 안드로이드 Toast 기능 라이브러리
https://github.com/scalessec/Toast

JSONKit - 써보니 그냥 편하고 빠른 JSON Parser
https://github.com/johnezang/JSONKit

AFNetworking - Network Framework
https://github.com/AFNetworking/AFNetworking

OpenCL 예제

OpenCL 예제이다. 이 예제에서는 GPU와 CPU를 동시에 사용하고 있다.

인텔 CPU에서 테스트를 했는데 인텔 OpenCL 드라이버를 설치하지 않으면, GPU만 가지고 수행을 하기 때문에 ret_num_platforms=1이 된다.

인텔 i5-2500과 지포스 GTX970에서 테스트 하였을때 ret_num_platforms=3이 되었다.

#include <stdio.h>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif

#define MEM_SIZE (128)
#define MAX_SOURCE_SIZE (0x100000)

int main()
{
cl_device_id device_cpu = NULL;
cl_device_id device_gpu = NULL;
cl_context context_cpu = NULL;
cl_context context_gpu = NULL;
cl_command_queue command_queue_cpu = NULL;
cl_command_queue command_queue_gpu = NULL;
cl_mem memobj_cpu = NULL;
cl_mem memobj_gpu = NULL;
cl_program program_cpu = NULL;
cl_program program_gpu = NULL;
cl_kernel kernel_cpu = NULL;
cl_kernel kernel_gpu = NULL;
cl_platform_id platform[2];
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret;

char string_cpu[MEM_SIZE];
char string_gpu[MEM_SIZE];

FILE *fp;
char fileName[] = "./hello.cl";
char *source_str;
size_t source_size;

/* 커널을 포함한 소스 코드를 로드 */
fp = fopen(fileName, "r");
if (!fp) {
fprintf(stderr, "Failed to load kernel.\n");
exit(1);
}
source_str = (char*)malloc(MAX_SOURCE_SIZE);
source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
fclose(fp);

/* 플랫폼, 디바이스 정보를 얻음 */
ret = clGetPlatformIDs(2, platform, &ret_num_platforms);
ret = clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_ALL, 1, &device_gpu, &ret_num_devices);
ret = clGetDeviceIDs(platform[1], CL_DEVICE_TYPE_ALL, 1, &device_cpu, &ret_num_devices);

/* OpenCL 컨텍스트 생성 */
context_gpu = clCreateContext(NULL, 1, &device_gpu, NULL, NULL, &ret);
context_cpu = clCreateContext(NULL, 1, &device_cpu, NULL, NULL, &ret);

/* 커맨드 큐 생성 */
command_queue_cpu = clCreateCommandQueue(context_cpu, device_cpu, 0, &ret);
command_queue_gpu = clCreateCommandQueue(context_gpu, device_gpu, 0, &ret);

/* 메모리 버퍼 생성 */
memobj_cpu = clCreateBuffer(context_cpu, CL_MEM_READ_WRITE, MEM_SIZE * sizeof(char), NULL, &ret);
memobj_gpu = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE, MEM_SIZE * sizeof(char), NULL, &ret);

/* 미리 로드한 소스 코드로 커널 프로그램을 생성 */
program_cpu = clCreateProgramWithSource(context_cpu, 1, (const char **)&source_str, (const size_t *)&source_size, &ret);
program_gpu = clCreateProgramWithSource(context_gpu, 1, (const char **)&source_str, (const size_t *)&source_size, &ret);

/* 커널 프로그램 빌드 */
ret = clBuildProgram(program_cpu, 1, &device_cpu, NULL, NULL, NULL);
ret = clBuildProgram(program_gpu, 1, &device_gpu, NULL, NULL, NULL);

/* OpenCL 커널 생성*/
kernel_cpu = clCreateKernel(program_cpu, "hello", &ret);
kernel_gpu = clCreateKernel(program_gpu, "hello", &ret);

/* OpenCL 커널 파라미터 설정 */
ret = clSetKernelArg(kernel_cpu, 0, sizeof(cl_mem), (void *)&memobj_cpu);
ret = clSetKernelArg(kernel_gpu, 0, sizeof(cl_mem), (void *)&memobj_gpu);

/* OpenCL 커널 실행 */
ret = clEnqueueTask(command_queue_cpu, kernel_cpu, 0, NULL, NULL);
ret = clEnqueueTask(command_queue_gpu, kernel_gpu, 0, NULL, NULL);

/* 실행 결과를 메모리 버퍼에서 얻음 */
ret = clEnqueueReadBuffer(command_queue_cpu, memobj_cpu, CL_TRUE, 0, MEM_SIZE * sizeof(char), string_cpu, 0, NULL, NULL);
ret = clEnqueueReadBuffer(command_queue_gpu, memobj_gpu, CL_TRUE, 0, MEM_SIZE * sizeof(char), string_gpu, 0, NULL, NULL);

/* 결과 출력 */
puts(string_cpu);
puts(string_gpu);

/* 종료 처리 */
ret = clFlush(command_queue_cpu);
ret = clFlush(command_queue_gpu);
ret = clFinish(command_queue_cpu);
ret = clFinish(command_queue_gpu);
ret = clReleaseKernel(kernel_cpu);
ret = clReleaseKernel(kernel_gpu);
ret = clReleaseProgram(program_cpu);
ret = clReleaseProgram(program_gpu);
ret = clReleaseMemObject(memobj_cpu);
ret = clReleaseMemObject(memobj_gpu);
ret = clReleaseCommandQueue(command_queue_cpu);
ret = clReleaseCommandQueue(command_queue_gpu);
ret = clReleaseContext(context_cpu);
ret = clReleaseContext(context_gpu);

free(source_str);

// while (1);
return 0;
}

CUDA 예제

#include "device_launch_parameters.h"
#include <cuda_runtime.h>
#include <stdlib.h>
#include <stdio.h>

#define SIZE 1024

// __global__을 통해서 커널임을 표시한다. host에서 호출된다.
__global__ void VectorAdd(int *a, int *b, int *c, int n)
{
    // 수많은 스레드가 동시에 처리한다.
    // 따라서 threadIdx(스레드 인덱스)를 통해서 스레드들을 구별한다.
    int i = threadIdx.x;
    printf("threadIdx.x : %d, n : %d\n", i, n);
    for (i = 0; i < n; i++)
    {
        c[i] = a[i] + b[i];
        printf("%d = %d + %d\n", c[i], a[i], b[i]);
    }
}

int main()
{
    int *a, *b, *c;
    int *d_a, *d_b, *d_c;

    // 호스트의 메모리에 할당한다.
    a = (int *)malloc(SIZE * sizeof(int));
    b = (int *)malloc(SIZE * sizeof(int));
    c = (int *)malloc(SIZE * sizeof(int));

    // cudaMalloc(destination, number of byte)로 device의 메모리를 할당한다.
    cudaMalloc(&d_a, SIZE * sizeof(int));
    cudaMalloc(&d_b, SIZE * sizeof(int));
    cudaMalloc(&d_c, SIZE * sizeof(int));

    // 초기화
    for (int i = 0; i < SIZE; ++i)
    {
        a[i] = i;
        b[i] = i;
        c[i] = 0;
    }

    // cudaMemcpy(destination, source, number of byte, cudaMemcpyHostToDevice)로 호스트에서 디바이스로 메모리를 카피한다.
    cudaMemcpy(d_a, a, SIZE * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(d_b, b, SIZE * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(d_c, c, SIZE * sizeof(int), cudaMemcpyHostToDevice);

    // 함수 호출을 위해서 새로운 신텍스 요소를 추가할 필요가 있다.
    // 첫번째 parameter는 블럭의 수이다. 예제에서는 스레드 블럭이 하나이다.
    // SIZE는 1024개의 스레드를 의미한다.
    VectorAdd<<<1, SIZE>>>(d_a, d_b, d_c, SIZE);

    //cudaMemcpy(source, destination, number of byte, cudaMemDeviceToHost)로 디바이스의 메모리(연산 결과 데이터)를 호스트에 카피한다.
    cudaMemcpy(a, d_a, SIZE * sizeof(int), cudaMemcpyDeviceToHost);
    cudaMemcpy(b, d_b, SIZE * sizeof(int), cudaMemcpyDeviceToHost);
    cudaMemcpy(c, d_c, SIZE * sizeof(int), cudaMemcpyDeviceToHost);
    for (int i = 0; i < SIZE; ++i)
        printf("c[%d] = %d\n", i, c[i]);

    // 호스트의 메모리 할당 해제
    free(a);
    free(b);
    free(c);

    // cudaFree(d_a)를 통해 디바이스의 메모리를 할당 해제
    cudaFree(d_a);
    cudaFree(d_b);
    cudaFree(d_c);
    return 0;
}