8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS Lambda で aobench を動かす(C/C++ binary を動かす)

Last updated at Posted at 2015-10-11

漢だったら AWS Lambda で C/C++ PaaS 的なことをやりたいですよね!

とりあえず aobench を AWS Lambda で動かしてみましょう.

を参考に, まずは手元で node.js アプリを構成します.

$ g++ -O2 -o ao ao.c
$ touch aobench.js
$ ls
ao aobench.js

async モジュールは今回は必要ありません.

aobench.js は以下のようになります.

var exec = require('child_process').exec;

exports.js = function(event, context) {
    console.log("running aobench...");
    child = exec('./ao', function(error) {
        console.log("running aobench done!");
        // Resolve with result of process
        context.done(error, 'Process complete!');
    });

    // Log process stdout and stderr
    child.stdout.on('data', console.log);
    child.stderr.on('data', console.error);
};

zip アップロードで実行する場合は, exports.h exports.js` 関数がエントリポイントになります.

出来たファイルを一式 zip 圧縮して AWS Lambda にアップロードします.
このとき, ディレクトリを zip 圧縮ではなく, ファイル群を zip 圧縮することに注意してください. そうしないと以下のようなエラーが出ます.

{
"errorMessage": "Cannot find module 'index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._resolveFilename (module.js:338:15)",
"Function.Module._load (module.js:280:25)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)"
]

実行する.

handler の部分に aobench.js(JS ファイル名)を指定して実行します. duration は長めに取ってきます(30 secs など)

2015-10-11T08:46:19.351Z	running aobench...
2015-10-11T08:46:19.493Z	running aobench done!
2015-10-11T08:46:23.594Z	ao: ao.c:324: void saveppm(const char*, int, int, unsigned char*): Assertion `fp' failed.

2015-10-11T08:46:23.734Z		TypeError: Object #<Object> has no method 'done'
    at /var/task/aobench.js:11:17
    at ChildProcess.exithandler (child_process.js:662:7)
    at ChildProcess.emit (events.js:98:17)
    at maybeClose (child_process.js:766:16)
    at Socket.<anonymous> (child_process.js:979:11)

おおっと! ファイルの書き込みはできないようですね...

とりあえずファイル書き出し無効にして aobench を再度コンパイルして, アップロードと実行します.

START RequestId: Version: $LATEST
2015-10-11T08:57:19.038Z	running aobench...
2015-10-11T08:57:23.634Z	aobench done

END RequestId: 
REPORT RequestId: 	Duration: 4656.21 ms	Billed Duration: 4700 ms 	Memory Size: 128 MB	Max Memory Used: 11 MB	

Voala!
C/C++ binary の実行に成功しました!

浮動小数点計算は?

構成を変えて実行してみました.

  • 0.35 secs : 手元環境での SandyBridge 3.3 GHz 1 core
  • 4.5 secs : AWS Lambda 128 MB
  • 1.1 secs : AWS Lambda 512 MB
  • 0.6 secs : AWS Lambda 1024 MB
  • 0.4 secs : AWS Lambda 1536 MB(設定できる最大値)

メモリサイズに応じて CPU 性能も変わるようですね. 浮動小数点性能は AWS Lambda 1.5 GB 時が概ね Desktop CPU 1 コア相当という感じでしょうか.

OpenMP は?

omp_get_max_threads() でスレッド数を取得してみました.

  • 128 MB : omp_num_threads = 2
  • 1536 MB : omp_num_threads = 2

HT(HyperThread)が有効な環境のようです. 1.5GB でも threads = 2 なのでマルチコア環境ではないようですね.

TODO

  • セキュリティ周りはどうなっているのかな?
  • C++ での計算結果をなにかしら返す仕組みを考える.
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?