X10動かしてみた

X10クラスタなどのハイエンドシステムでの高い生産性かつ、高速な動作を目指した言語らしいです。現在IBMにおいて処理系がオープンソースで開発されています。研究室にX10の開発者がやってくるというので、予習をすることになりました。さっそく動かしてみます。

ソースからのビルド

ビルドには

が必要なので事前にインストールしておきます。

私の環境はLinux(Ubuntu 7.10), Java 1.6となっています。また、X10の処理系は現時点で最新のversion 1.5(2007/06/29))をインストールしてます。

まずはhttp://sourceforge.net/project/showfiles.php?group_id=181722からx10-1.5-src.zipをダウンロードします。

ファイルを展開後、x10.common, x10.compiler, x10.runtime という三つのディレクトリができるので、各ディレクトリの中で

ant

コマンドを実行してビルドます。x10の動作には polyglotなどが必要ですが、セットアップスクリプトで自動で取得してきてくれています。

と本来はこれで完了のはずですが、x10.compilerのビルドで失敗します。理由は,ビルドスクリプトがpolyglotの最新バージョン(2.3.0)をダウンロードしてきますが、このバージョン(2.3.0)ではビルドできません。
なので、http://www.cs.cornell.edu/projects/polyglot/ からバージョン2.2.3をダウンロードします。

polyglotは展開してできたディレクトリで

ant jar

とコマンドを実行すればビルドできます。

すると、polyglot.jarができるので、x10.common/lib ディレクトリに polyglot2.jar という名前でコピーします。そして、x10.compiler ディレクトリで

ant

を実行します。ビルドに成功するはずです。

ここまで来ると x10.common/bin ディレクトリは

junit
junit.in
newpgrp
setupX10
setupX10.in
testScript
testScript0
x10
x10.in
x10c
x10c.in

という構成になっているはずです。x10がランタイムで、x10cがコンパイラですので、このパスを適宜実行パスに追加しておきます。

testScriptスクリプトを使ってセットアップが正常にできているかを確認してみます。このスクリプトはいわゆるユニットテストのようなことができるようです。
実行は x10.common/examples ディレクトリで

../bin/testScript

とコマンドを実行します。

結果は以下のように出力されました。

Warning: directory ../../x10.web/html/testLogs was not found
Starting: ../bin/testScript0
Machine name: cure
Operating system: Linux
Java home: /usr/lib/jvm/java-6-sun
Java version: java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)
Current directory: /home/taisuke/lib/x10.common/examples
System load: 11:21:50 up 25 days, 23:13, 5 users, load average: 0.18, 0.50, 0.60

Misc TypeElaborationAcrossCompilationUnits met expectation: Success.
Misc FinalInitializationTest met expectation: Success.
Misc BreakInForTest met expectation: Success.
Misc Unreachable3_MustFailTimeout met expectation: FailTimeout.
Misc StringTest met expectation: Success.
Misc Unreachable met expectation: Success.
Misc AssignIntToChar met expectation: Success.
Misc NopTest met expectation: Success.
Misc TabsInStrings met expectation: Success.
.....

Hello, World!

無事セットアップが完了したので、早速動かしてみます。x10のソースファイルの拡張子はどうやら ".x10"とするようです。

まずは、ソースコードを用意。名前は hello.x10 にしました。

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}

コンパイル

$ x10c hello.x10

実行します。実行する時にはクラス名を指定すればいいみたいです。

$ x10 HelloWorld
Hello, world!

できた! ファイル名とクラス名は一致していなくても大丈夫だった。

日本語はどうだろう。

public class HelloWorld {
public static void main(String[] args) {
System.out.println("はろーわーるど");
}
}

実行すると

$ x10 HelloWorld
?????????????????????

だめだな。

Hello worldコンパイル結果を覗いてみる

(追記 2008/04/25 クラスファイルを逆コンパイルなんてしなくても、Javaファイルが生成されていました)

Javaの逆コンパイラhttp://www.kpdus.com/jad.htmltitle=jadを使ってどんな風にコンパイルされたのかを覗いてみます。コンパイラにより生成されたのは

  • HelloWorld.class
  • HelloWorld$Main.class

で、下の方はインナークラスのファイルになっています。

jad HelloWorld.class

とすると、HelloWorld.jadというテストファイルができて中身はこうなりました。

// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   HelloWorld.java

import java.io.PrintStream;
import x10.lang.Object;
import x10.lang.Runtime;
import x10.runtime.Activity;

public class HelloWorld extends x10.lang.Object
{
    public static class Main extends Activity
    {

        public void runX10Task()
        {
            HelloWorld.main(form);
        }

        private final String form[];

        public Main(String as[])
        {
            super("Main Activity");
            form = as;
        }
    }


    public static void main(String args[])
    {
        if(Runtime.runtime == null)
        {
            System.err.println("Please use the 'x10' script to invoke X10 programs, or see the generated");
            System.err.println("Java code for alternate invocation instructions.");
            System.exit(128);
        }
        ((PrintStream)Runtime.hereCheck(System.out)).println("Hello, world!");
    }

    public HelloWorld()
    {
    }
}

メインのアクティビティが一つあるのが分かる。ソースを追ってみると、

  1. x10.lang.Runtime.java の main(String[]) から
  2. デフォルトのランタイム設定の場合 x10.runtime.DefaultRuntime_cが作られ,そのrun(String[])メソッドで
  3. メインのアクティビティ, HelloWorld$Main が作られ,
  4. (たぶん)x10.runtime.LocalPlace_cのrunAsync(Activity) -> runActivity(Activity) が呼ばれ
  5. スレッドプールのexecuteで Activity.run()が別スレッドで起動し,
  6. Actiivtyのrun()ではx10.runtime.InvocationStrategy$DefaultStrategyからHelloWorld$Main.runX10Taskが呼ばれる。

だいたい、こんな順序で実行されています。