+ All Categories
Home > Education > Introduction to julia language

Introduction to julia language

Date post: 27-Jan-2015
Category:
Upload: qiang-kou
View: 124 times
Download: 6 times
Share this document with a friend
Description:
Slides for Journal Club in SYSU
Popular Tags:
49
Introduction to Julia Language Kou Qiang Sun Yat-sen University December 6, 2012 Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 1 / 33
Transcript
Page 1: Introduction to julia language

Introduction to Julia Language

Kou Qiang

Sun Yat-sen University

December 6, 2012

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 1 / 33

Page 2: Introduction to julia language

Outline

Outline

1 Overview

2 FeaturesPerformanceParallelismCloud Computing

3 LLVM and Clang

4 Comparison between R and JuliaAdvantages and DisadvantagesThe State of Statistics in Julia

5 Installation and Usage

6 An Example

7 Other Projects

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 2 / 33

Page 3: Introduction to julia language

Overview

The blog of Douglas Bates

http://dmbates.blogspot.com/2012/04/r-programmer-looks-at-julia.html

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 3 / 33

Page 4: Introduction to julia language

Overview

The Homepage

http://julialang.org/

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 4 / 33

Page 5: Introduction to julia language

Overview

Syntax

function mandel(z)

c = z

maxiter = 80

for n = 1: maxiter

if abs(z) > 2

return n-1

end

z = z^2 + c

end

return maxiter

end

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 5 / 33

Page 6: Introduction to julia language

Features Performance

Features

1 Overview

2 FeaturesPerformanceParallelismCloud Computing

3 LLVM and Clang

4 Comparison between R and JuliaAdvantages and DisadvantagesThe State of Statistics in Julia

5 Installation and Usage

6 An Example

7 Other Projects

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 6 / 33

Page 7: Introduction to julia language

Features Performance

Performance

benchmark times relative to C++ (smaller is better)

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 7 / 33

Page 8: Introduction to julia language

Features Performance

R

> fun1r = function () {

+ m = matrix(0, 1000, 1000)

+ s = 0

+ for(i in 1:1000)

+ {

+ for(j in 1:1000)

+ {

+ m[i, j] = runif (1)

+ s = s + m[i, j]

+ }

+ }

+ }

> system.time(fun1r())

user system elapsed

8.113 0.006 8.120

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 8 / 33

Page 9: Introduction to julia language

Features Performance

Julia

julia > function fun1j()

m = zeros (1000 , 1000)

s = 0

for i = 1:1000

for j = 1:1000

m[i, j] = rand()

s = s + m[i, j]

end

end

end

julia > @elapsed fun1j()

0.22739791870117188

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 9 / 33

Page 10: Introduction to julia language

Features Performance

R

> fun1optr = function () {

+ m = matrix(runif (1000 * 1000) , 1000, 1000)

+ sum(m)

+ }

> system.time(fun1optr ())

user system elapsed

0.132 0.003 0.136

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 10 / 33

Page 11: Introduction to julia language

Features Performance

Julia

julia > function fun1optj ()

m = rand (1000 , 1000)

sum(m)

end

julia > @elapsed fun1optj ()

0.010874032974243164

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 11 / 33

Page 12: Introduction to julia language

Features Performance

Julia

julia > 1//2

1//2

julia > 11//22

1//2

julia > 987//654 + 12//34 * 56//78

84917//48178

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 12 / 33

Page 13: Introduction to julia language

Features Parallelism

Features

1 Overview

2 FeaturesPerformanceParallelismCloud Computing

3 LLVM and Clang

4 Comparison between R and JuliaAdvantages and DisadvantagesThe State of Statistics in Julia

5 Installation and Usage

6 An Example

7 Other Projects

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 13 / 33

Page 14: Introduction to julia language

Features Parallelism

Parallelism

$ ./ julia -p 2

M = {rand (1000 ,1000) | i=1:10}

pmap(svd , M)

nheads = @parallel (+) for i=1:100000000

randbit ()

end

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 14 / 33

Page 15: Introduction to julia language

Features Cloud Computing

Features

1 Overview

2 FeaturesPerformanceParallelismCloud Computing

3 LLVM and Clang

4 Comparison between R and JuliaAdvantages and DisadvantagesThe State of Statistics in Julia

5 Installation and Usage

6 An Example

7 Other Projects

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 15 / 33

Page 16: Introduction to julia language

Features Cloud Computing

Cloud Computing

http://julia.forio.com/

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 16 / 33

Page 17: Introduction to julia language

LLVM and Clang

LLVM and Clang

LLVM: Low Level Virtual Machine

Clang: an “LLVM native” C/C++/Objective-C compiler

JIT compilation: Just-in-time compilation

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 17 / 33

Page 18: Introduction to julia language

LLVM and Clang

LLVM and Clang

LLVM: Low Level Virtual Machine

Clang: an “LLVM native” C/C++/Objective-C compiler

JIT compilation: Just-in-time compilation

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 17 / 33

Page 19: Introduction to julia language

LLVM and Clang

LLVM and Clang

LLVM: Low Level Virtual Machine

Clang: an “LLVM native” C/C++/Objective-C compiler

JIT compilation: Just-in-time compilation

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 17 / 33

Page 20: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Comparison between R and Julia

1 Overview

2 FeaturesPerformanceParallelismCloud Computing

3 LLVM and Clang

4 Comparison between R and JuliaAdvantages and DisadvantagesThe State of Statistics in Julia

5 Installation and Usage

6 An Example

7 Other Projects

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 18 / 33

Page 21: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Advantages

A JIT

A richer type system

Multiple dispatch

Parallelism

A cleaner model for accessing libraries of compiled code

Memory-mapped I/O

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 19 / 33

Page 22: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Advantages

A JIT

A richer type system

Multiple dispatch

Parallelism

A cleaner model for accessing libraries of compiled code

Memory-mapped I/O

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 19 / 33

Page 23: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Advantages

A JIT

A richer type system

Multiple dispatch

Parallelism

A cleaner model for accessing libraries of compiled code

Memory-mapped I/O

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 19 / 33

Page 24: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Advantages

A JIT

A richer type system

Multiple dispatch

Parallelism

A cleaner model for accessing libraries of compiled code

Memory-mapped I/O

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 19 / 33

Page 25: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Advantages

A JIT

A richer type system

Multiple dispatch

Parallelism

A cleaner model for accessing libraries of compiled code

Memory-mapped I/O

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 19 / 33

Page 26: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Advantages

A JIT

A richer type system

Multiple dispatch

Parallelism

A cleaner model for accessing libraries of compiled code

Memory-mapped I/O

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 19 / 33

Page 27: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Disadvantages

Default arguments of functions and named actual arguments infunction calls

The R package system and CRAN

Graphics

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 20 / 33

Page 28: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Disadvantages

Default arguments of functions and named actual arguments infunction calls

The R package system and CRAN

Graphics

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 20 / 33

Page 29: Introduction to julia language

Comparison between R and Julia Advantages and Disadvantages

Disadvantages

Default arguments of functions and named actual arguments infunction calls

The R package system and CRAN

Graphics

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 20 / 33

Page 30: Introduction to julia language

Comparison between R and Julia The State of Statistics in Julia

Comparison between R and Julia

1 Overview

2 FeaturesPerformanceParallelismCloud Computing

3 LLVM and Clang

4 Comparison between R and JuliaAdvantages and DisadvantagesThe State of Statistics in Julia

5 Installation and Usage

6 An Example

7 Other Projects

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 21 / 33

Page 31: Introduction to julia language

Comparison between R and Julia The State of Statistics in Julia

The State of Statistics in Julia

DataFrames

Distributions

Optim

MCMC

NHST: null hypothesis significance testing

Clustering: k-means clustering

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 22 / 33

Page 32: Introduction to julia language

Comparison between R and Julia The State of Statistics in Julia

The State of Statistics in Julia

DataFrames

Distributions

Optim

MCMC

NHST: null hypothesis significance testing

Clustering: k-means clustering

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 22 / 33

Page 33: Introduction to julia language

Comparison between R and Julia The State of Statistics in Julia

The State of Statistics in Julia

DataFrames

Distributions

Optim

MCMC

NHST: null hypothesis significance testing

Clustering: k-means clustering

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 22 / 33

Page 34: Introduction to julia language

Comparison between R and Julia The State of Statistics in Julia

The State of Statistics in Julia

DataFrames

Distributions

Optim

MCMC

NHST: null hypothesis significance testing

Clustering: k-means clustering

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 22 / 33

Page 35: Introduction to julia language

Comparison between R and Julia The State of Statistics in Julia

The State of Statistics in Julia

DataFrames

Distributions

Optim

MCMC

NHST: null hypothesis significance testing

Clustering: k-means clustering

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 22 / 33

Page 36: Introduction to julia language

Comparison between R and Julia The State of Statistics in Julia

The State of Statistics in Julia

DataFrames

Distributions

Optim

MCMC

NHST: null hypothesis significance testing

Clustering: k-means clustering

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 22 / 33

Page 37: Introduction to julia language

Installation and Usage

Installation and Usage

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 23 / 33

Page 38: Introduction to julia language

An Example

Rosenbrock Function Optimization

f (x , y) = (1 − x)2 + 100(y − x2)2

A global minimum at (x , y) = (1, 1) where f (x , y) = 0

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 24 / 33

Page 39: Introduction to julia language

An Example

Rosenbrock Function Optimization

load("src/Optim.jl")

using Optim

function rosenbrock(x:: Vector)

(1.0-x[1]) ^2+100.0*(x[2]-x[1]^2) ^2

end

function rosenbrock_gradient(x:: Vector)

[ -2.0*(1.0 -x[1]) -400.0*(x[2]-x[1]^2)*x[1] ,200.0*(x[2]-x[1]^2)]

end

function rosenbrock_hessian(x:: Vector)

h = zeros(2, 2)

h[1 ,1]=2.0 -400.0*x[2]+1200.0*x[1]^2

h[1 ,2]= -400.0*x[1]

h[2 ,1]= -400.0*x[1]

h[2 ,2]=200.0

h

end

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 25 / 33

Page 40: Introduction to julia language

An Example

Rosenbrock Function Optimization

problem = Dict()

problem [:f] = rosenbrock

problem [:g] = rosenbrock_gradient

problem [:h] = rosenbrock_hessian

problem [: initial_x] = [0.0, 0.0]

problem [: solution] = [1.0, 1.0]

algorithms = [" naive_gradient_descent",

"gradient_descent",

"newton",

"bfgs",

"l-bfgs",

"nelder -mead",

"sa"]

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 26 / 33

Page 41: Introduction to julia language

An Example

Rosenbrock Function Optimization

for algorithm = algorithms

results = optimize(problem [:f],

problem [:g],

problem [:h],

problem [: initial_x],

algorithm ,

10e-8,

true)

print(results)

end

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 27 / 33

Page 42: Introduction to julia language

Other Projects

GraphChi: Disk-based large-scale graph computation

“Big Data - small machine”

Implemented in plain C++ and open-source

Support: ALS, SGD, bias-SGD, SVD, NMF, SVD++

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 28 / 33

Page 43: Introduction to julia language

Other Projects

GraphChi: Disk-based large-scale graph computation

“Big Data - small machine”

Implemented in plain C++ and open-source

Support: ALS, SGD, bias-SGD, SVD, NMF, SVD++

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 28 / 33

Page 44: Introduction to julia language

Other Projects

GraphChi: Disk-based large-scale graph computation

“Big Data - small machine”

Implemented in plain C++ and open-source

Support: ALS, SGD, bias-SGD, SVD, NMF, SVD++

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 28 / 33

Page 45: Introduction to julia language

Other Projects

GraphChi: Disk-based large-scale graph computation

A Mac Mini with 8 GB of RAM and 256 GB SSD drivehttp://code.google.com/p/graphchi/

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 29 / 33

Page 46: Introduction to julia language

Other Projects

CUDA

Device FLOPS Price(RMB)

Intel Core i7-980 XE 107.6G 2500∼3500nVIDIA Geforce GTX 690 about 6T 6500∼8000nVIDIA Geforce GT 430 91.564G 250∼450nVIDIA Tesla C2050 1.03T 6000∼8500

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 30 / 33

Page 47: Introduction to julia language

Other Projects

PyCUDA

http://documen.tician.de/pycuda/

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 31 / 33

Page 48: Introduction to julia language

Other Projects

PyCUDA

import pycuda.autoinit

import pycuda.driver as drv

import numpy

from pycuda.compiler import SourceModule

mod = SourceModule("""

__global__ void multiply_them (float *dest , float *a, float *b)

{

const int i = threadIdx.x;

dest[i] = a[i] * b[i];

}

""")

multiply_them = mod.get_function("multiply_them")

a = numpy.random.randn (400).astype(numpy.float32)

b = numpy.random.randn (400).astype(numpy.float32)

dest = numpy.zeros_like(a)

multiply_them(

drv.Out(dest), drv.In(a), drv.In(b),

block =(400 ,1 ,1), grid =(1 ,1))

print dest -a*b

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 32 / 33

Page 49: Introduction to julia language

Thanks

Thanks

Thank you for your time!

Kou Qiang (Sun Yat-sen University) Introduction to Julia Language December 6, 2012 33 / 33


Recommended