惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
博客园_首页
S
SegmentFault 最新的问题
H
Help Net Security
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss
量子位
GbyAI
GbyAI
M
MIT News - Artificial intelligence
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
B
Blog
月光博客
月光博客
博客园 - 聂微东
Vercel News
Vercel News
罗磊的独立博客
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
Arctic Wolf
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
L
Lohrmann on Cybersecurity
I
Intezer
小众软件
小众软件
T
The Exploit Database - CXSecurity.com
Jina AI
Jina AI
C
Check Point Blog
AWS News Blog
AWS News Blog
C
Cisco Blogs
Martin Fowler
Martin Fowler
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
宝玉的分享
宝玉的分享
S
Security Affairs
大猫的无限游戏
大猫的无限游戏
N
News and Events Feed by Topic
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Hacker News: Front Page
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog

谭升的博客

一封来自读者的信 【杂文】中美人工智能对比(一篇博人关注的软文) 【强化学习】2.2 行为评价方法(Action-value Methods) 【Hexo】Hexo下next主题valine强化版本的改造 【强化学习】2.1 k臂赌博机(k-armed bandits)问题 【强化学习】2.0 多臂赌博机 【Julia】整型和浮点型数字 【强化学习】1.6 本章总结、强化学习历史简述 【Julia】变量 【Julia】Julia环境搭建(Mac,Windows,Linux) 【强化学习】 1.5 强化学习的一个扩展举例 【强化学习】 1.4.1 强化学习与优化方法 【Julia】Julia编程语言介绍 【信息论、推理与学习算法】本系列博客介绍 【强化学习】 1.4.0 “进化方法”和 “决策梯度方法” 概论 【强化学习】1.3 强化学习的基础元素 【强化学习】1-2 强化学习举例 【强化学习】1-1-4 强化学习和人工智能 【强化学习】1-1-3 强化学习基本框架
【Julia】开始使用Julia
本文作者: 谭升 · 2018-10-01 · via 谭升的博客

Abstract: 本文介绍如何在命令行中开启、使用、退出Julia,如何执行文件,以及执行Julia时候的可选选项(参数列表)
Keywords: Julia,Julia使用,Julia命令行,Julia执行文件

本文我们来学习Julia的几种用法,包括便捷的终端交互,以及对于复杂功能的文件执行。

终端使用Julia(交互模式)

在终端启动Julia比较简单,上文我们完成了Julia的安装,并在Mac下完成了命令行下启动的设置,那么我们可直接在命令行提示符后输入julia 完成Julia环境的启动(交互模式),如果出现下图,表示启动成功了。

在交互模式下执行命令

在Julia的命令提示符后输入1 + 2 后回车,就会显示计算结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ julia

_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.0.0 (2018-08-08)
_/ |\__'_|_|_|\__'_| |
|__/ |


julia> 1 + 2
3

julia> ans
3

ans变量表示上次计算的结果,如果在这个ans下再次输入ans并回车,显示结果是:

1
2
3
4
5
6
7
8
julia> 1 + 2
3

julia> ans
3

julia> ans
3

注意:ans只能在交互模式下使用

在交互模式下执行文件

如果你有一个julia文件 file.jl,里面写的是计算过程:

1
1+3

你可以在交互模式下执行这个文件,使用命令 include(“file.jl”)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.0.0 (2018-08-08)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> include("file.jl")
4

julia>

退出交互模式

退出交互模式回到终端有两种方法:

  1. CTRL-d linux和windows下同时按下 ctrl 键和d
  2. 输入 exit()

终端执行Julia文件

在终端下执行Julia文件的方式和其他脚本的执行方式类似:

1
$ julia script.jl arg1 arg2...

如果我们执行上面我们说到的 file.jl那么在终端下的执行结果如下:

1
2
$ julia file.jl
$

没有显示结果,因为我们在文件中只有 1+3这条指令,而没有要求他输出什么,在交互模式下,程序自动显示计算结果,但是在终端下执行脚本,只执行脚本中的命令,而不会自己显示什么。
接着我们编辑另一个文件script.jl,内容如下:

1
2
3
4
println(PROGRAM_FILE);
for x in ARGS;
println(x);
end

然后我们加上参数,这里是输入给脚本的参数 —— arg1 arg2 …

1
2
3
4
5
$ julia script.jl foo bar
script.jl
foo
bar
$

这里的三部分分别是 julia (程序名,用于在终端中启动程序,类似于python脚本运行前的python,sh等)、 script.jl(脚本文件名) 以及 foo bar (输入脚本的参数)。

Julia的参数

在执行脚本时我们给脚本了两个个参数(foo bar),同时,我们也可以给julia多个参数(上面的例子中 script.jl foo bar 是Julia程序的一个参数 ),加参数方式是在多个参数中间用 划分 ,例如上面的例子可以加上如下参数

1
julia --color=yes -O --script.jl foo bar

julia程序包含两个参数,分别是:

  • –color=yes -O
  • –script.jl foo bar

script.jl 包含两个参数:

  • foo
  • bar

Julia 的并行模式(本机和集群)

Julia可以以并行模式启动,启动参数是 -p或者 –machine-file
-p n 将会启动n个工作进程
–machine-file file 将会按照file中记录的机器地址,启动对应机器上的任务。
–machine-file 模式注意以下要求:

  1. 这些机器的登录方式必须是ssh无密码登录(不需要输入手工输入密码,而是通过ssh 秘钥登录)
  2. 这些机器上的Julia安装目录必须和当前执行命令的主机位置相同
  3. file中的每一条机器记录格式如下
    • [count*][user@]host[:port] [bind_addr[:port]]
      • count *是节点要执行的工作进程数量(类似本机的-p n)
      • user 默认是当前用户
      • port 是标准ssh的端口号,默认是1
      • bind-to bind_addr[:port] 是其他机器使用的IP地址和端口号,可以用来连接到本机。

Julia更多执行参数列表

Julia的更多参数列表如下,执行方法:

1
julia [switches] -- [programfile] [args...]

参数列表:

SwitchDescription
-v, –versionDisplay version information
-h, –helpPrint this message
-J, –sysimage <file>Start up with the given system image file
-H, –home <dir>Set location of julia executable
–startup-file={yes|no}Load ~/.julia/config/startup.jl
–handle-signals={yes|no}Enable or disable Julia’s default signal handlers
–sysimage-native-code={yes|no}Use native code from system image if available
–compiled-modules={yes|no}Enable or disable incremental precompilation of modules
-e, –eval <expr>Evaluate <expr>
-E, –print <expr>Evaluate <expr> and display the result
-L, –load <file>Load <file> immediately on all processors
-p, –procs {N|auto}Integer value N launches N additional local worker processes; auto launches as many workers as the number of local CPU threads (logical cores)
–machine-file <file>Run processes on hosts listed in <file>
-iInteractive mode; REPL runs and isinteractive() is true
-q, –quietQuiet startup: no banner, suppress REPL warnings
–banner={yes|no|auto}Enable or disable startup banner
–color={yes|no|auto}Enable or disable color text
–history-file={yes|no}Load or save history
–depwarn={yes|no|error}Enable or disable syntax and method deprecation warnings (error turns warnings into errors)
–warn-overwrite={yes|no}Enable or disable method overwrite warnings
-C, –cpu-target <target>Limit usage of cpu features up to <target>; set to help to see the available options
-O, –optimize={0,1,2,3}Set the optimization level (default level is 2 if unspecified or 3 if used without a level)
-g, -g <level>Enable / Set the level of debug info generation (default level is 1 if unspecified or 2 if used without a level)
–inline={yes|no}Control whether inlining is permitted, including overriding @inline declarations
–check-bounds={yes|no}Emit bounds checks always or never (ignoring declarations)
–math-mode={ieee,fast}Disallow or enable unsafe floating point optimizations (overrides @fastmath declaration)
–code-coverage={none|user|all}Count executions of source lines
–code-coverageequivalent to –code-coverage=user
–track-allocation={none|user|all}Count bytes allocated by each source line
–track-allocationequivalent to –track-allocation=user

总结

本文介绍Julia基础的启动,以及并行模型,执行参数的选择等知识。

Reference

  1. https://docs.julialang.org/en/v1/manual/getting-started/

原文地址: https://face2ai.com/Julia-Lang-2-Getting-Started