Cucco’s Compute Hack

コンピュータ関係の記事を書いていきます。

Failed to create session. CUDA_ERROR_INVALID_DEVICE

古いGPUがついていると、こんなエラーになることがある。

$cd /usr/local/lib/python3.5/dist-packages/tensorflow/models/image/mnist
$/usr/local/lib/python3.5/dist-packages/tensorflow/models/image/mnist$ python3 convolutional.py
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.so locally
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties:
name: GeForce GTX 1070
major: 6 minor: 1 memoryClockRate (GHz) 1.7465
pciBusID 0000:01:00.0
Total memory: 7.92GiB
Free memory: 7.79GiB
W tensorflow/stream_executor/cuda/cuda_driver.cc:590] creating context when one is currently active; existing: 0x314aa00
E tensorflow/core/common_runtime/direct_session.cc:135] Internal: failed initializing StreamExecutor for CUDA device ordinal 1: Internal: failed call to cuDevicePrimaryCtxRetain: CUDA_ERROR_INVALID_DEVICE
Traceback (most recent call last):
  File "convolutional.py", line 339, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/app.py", line 43, in run
    sys.exit(main(sys.argv[:1] + flags_passthrough))
  File "convolutional.py", line 284, in main
    with tf.Session() as sess:
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1186, in __init__
    super(Session, self).__init__(target, graph, config=config)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 551, in __init__
    self._session = tf_session.TF_NewDeprecatedSession(opts, status)
  File "/usr/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/errors_impl.py", line 469, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InternalError: Failed to create session.
 

こうやって実行すれば、GPUを0だけ使用して、実行する。

$CUDA_VISIBLE_DEVICES=0 python3 convolutional.py

以下でも可。

$export CUDA_VISIBLE_DEVICES=0
$python3 convolutional.py

以下の定数も必要な様子。

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda

CUCA Toolkitのインストール (network deb)

Ubuntu 16.04にCUCA Toolkitのインストールした時のメモ。
CUDA ToolkitCUDA 8.0 Downloads https://developer.nvidia.com/cuda-downloads

Select Target Platformから
Operating System:Linux
Architecture:x86_64
Distribution:Ubuntu
Version:16.04

Installer Typeで、[deb (network)]を選ぶと、
実行手順として以下を実行しろ、と言われる。
cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb自体は、wgetでローカルに保存しておく。

wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
Installation Instructions:
1. sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
2. sudo apt-get update
3. sudo apt-get install cuda

Enterキーが押せない

Ubuntu16.04のインストールの話。
最後に、メディアを抜いて、Enterキーを押せ、と出る。
Please remove installation media and close tray and press enter.

しかし、それまで使えていたUSBのキーボードは認識しない様子。
困り果てて、しばらく(10分くらい?)放置してたら自動的に再起動したっぽい。

PS/2の端子のあるキーボードで押せるかどうか。。。

CUCA Toolkitのインストール

CUCA Toolkitのインストール

Ubuntu 16.04にCUCA Toolkitのインストールした時のメモ。
CUDA ToolkitCUDA 8.0 Downloads https://developer.nvidia.com/cuda-downloads

[Select Target Platform]から
Operating System:Linux
Architecture:x86_64
Distribution:Ubuntu
Version:16.04

Installer Typeで、[deb (network)]を選ぶと、
実行手順として以下を実行しろ、と言われるが。。。

Installation Instructions:
1. `sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb`
2. `sudo apt-get update`
3. `sudo apt-get install cuda`

エラーとなる。

$ sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
dpkg: アーカイブ cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb の処理中 にエラーが発生しました (--install):
 アーカイブにアクセスできません: そのようなファイルやディレクトリはありません
処理中にエラーが発生しました:
 cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb

仕方がないので、Installer Typeをrunfile (local)に変更してファイルをダウンロード。バージョンは適宜書き換え。

wget https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run
Installation Instructions:
1. Run `sudo sh cuda_8.0.61_375.26_linux.run`
2. Follow the command-line prompts

1.を実行すると、以下などを聞かれる。

  1. ドライバのインストール
  2. サンプルのインストール

CUCA Toolkitのインストールエラーのメモ

まだ整理できてない。
こんなコマンドを打っている

sudo ln -sf /usr/lib/x86_64-linux-gnu/libGL.so.1 /usr/lib/libGL.so.1
sudo sh cuda_8.0.61_375.26_linux-run -silent -driver

Ubuntu 16.04 NVIDIA driver Install

Ubuntu16.04インストール直後は、GPUのドライバがないので、ドライバを更新する。
ログイン画面が開かない場合は、仮想コンソールを開く。Ctrl + Alt + F1。
次のようなエラー(LVMの場合)?の場合も対象。

lvmetad is not active yet; using direct activation during sysinit

インストールするドライバをリポジトリから探す

$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt-get update
$ sudo apt-cache search nvidia-3
nvidia-304-dev - NVIDIA binary Xorg driver development files
nvidia-331 - Transitional package for nvidia-331
nvidia-331-dev - Transitional package for nvidia-340-dev
nvidia-331-updates - Transitional package for nvidia-340
nvidia-331-updates-dev - Transitional package for nvidia-340-dev
…
nvidia-375 - NVIDIA binary driver - version 375.26

インストール

NVIDIAのサイトでも配っていたversion 375.26をインストール。

$ sudo apt-get install nvidia-375

SQL操作で列の最大値を得る

ある列が最大の列の値を取ってくる場合、以下は間違い。

mysql> select id,time from table_sec where time=max(time);
ERROR 1111 (HY000): Invalid use of group function

列名に対してMAXを適用するなんて不思議な感じ。

mysql> select id,max(time) from table_sec;
+----+---------------------+
| id | max(time)           |
+----+---------------------+
|  1 | 2016-12-12 11:00:15 |
+----+---------------------+
1 row in set (0.00 sec)

mysqlでのテーブルの1対多のjoin。

mysqlでのテーブルの1対多のjoin。

TABLE_SECには、5秒ごとのデータが入っている。
TABLE_HOURには、1時間ごとのデータが入っている。

10:00:06の時点では、10:00:00からの5秒間のデータは確定しているが、
10:00:00から1時間のデータは確定していない。
そのため、9:00:00から1時間のデータを参考の資料として対応づける。

TABLE_SECでは、timeShift1、timeShift2は使用していないが、同じテーブル構成になるように定義を残している。
TABLE_HOURでは、timeShift1は1時間先、timeShift2は2時間先の時刻を指している。

INNER JOINで2つの条件で1件に絞ってJOINしている。

# -*- coding: utf-8

import mysql.connector
import datetime

config = {
    'user': 'root',
    'password': 'Password',
    'host': 'localhost',
    'database':'testdb',
    'charset':'utf8'
}
cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)

sql = 'DROP TABLE IF EXISTS TABLE_SEC;'
cur.execute(sql)
cnx.commit()

sql = 'DROP TABLE IF EXISTS TABLE_HOUR;'
cur.execute(sql)
cnx.commit()

sql = 'CREATE TABLE IF NOT EXISTS TABLE_SEC (id INT UNSIGNED NOT NULL AUTO_INCREMENT,\
							time DATETIME DEFAULT NULL, \
							timeShift1 DATETIME DEFAULT NULL, \
							timeShift2 DATETIME DEFAULT NULL, \
							volume INT UNSIGNED DEFAULT NULL,\
							valid INT UNSIGNED DEFAULT NULL ,\
							primary key(id),\
							unique (time));'
cur.execute(sql)
cnx.commit()

sql = 'CREATE TABLE IF NOT EXISTS TABLE_HOUR (id INT UNSIGNED NOT NULL AUTO_INCREMENT,\
							time DATETIME DEFAULT NULL, \
							timeShift1 DATETIME DEFAULT NULL, \
							timeShift2 DATETIME DEFAULT NULL, \
							volume INT UNSIGNED DEFAULT NULL,\
							valid INT UNSIGNED DEFAULT NULL ,\
							primary key(id),\
							unique (time));'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_SEC (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T9:59:55","2016-12-12T10:00:00","2016-12-12T10:00:5",1,0);'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_SEC (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T10:00:00","2016-12-12T10:00:05","2016-12-12T10:00:10",1,1);'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_SEC (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T10:00:05","2016-12-12T10:00:10","2016-12-12T10:00:15",1,2);'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_SEC (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T11:00:10","2016-12-12T11:00:15","2016-12-12T11:00:20",1,3);'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_SEC (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T11:00:15","2016-12-12T11:00:20","2016-12-12T11:00:25",1,4);'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_SEC (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T10:00:20","2016-12-12T10:00:25","2016-12-12T10:00:30",1,5);'
cur.execute(sql)
cnx.commit()

sql='INSERT INTO TABLE_HOUR (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T8:00:00","2016-12-12T9:00:00","2016-12-12T10:00:00",2,8);'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_HOUR (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T9:00:00","2016-12-12T10:00:00","2016-12-12T11:00:00",2,9);'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_HOUR (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T10:00:00","2016-12-12T11:00:00","2016-12-12T12:00:00",2,10);'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_HOUR (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T11:00:00","2016-12-12T12:00:00","2016-12-12T13:00:00",2,11);'
cur.execute(sql)
cnx.commit()
sql='INSERT INTO TABLE_HOUR (time,timeShift1,timeShift2,volume,valid) VALUES("2016-12-12T12:00:00","2016-12-12T13:00:00","2016-12-12T14:00:00",2,12);'
cur.execute(sql)
cnx.commit()

#ここからが本題。
#SELECT * FROM TABLE_HOUR INNER JOIN TABLE_SEC ON TABLE_SEC.time >=TABLE_HOUR.timeShift1 AND TABLE_SEC.time < TABLE_HOUR.timeShift2;
sql='SELECT TABLE_SEC.id,TABLE_SEC.time,TABLE_HOUR.time FROM TABLE_HOUR INNER JOIN TABLE_SEC ON TABLE_SEC.time >=TABLE_HOUR.timeShift1 AND TABLE_SEC.time < TABLE_HOUR.timeShift2;'

cur.execute(sql)
record = cur.fetchone()
while record!=None:
	print(record)
	record = cur.fetchone()

cur.close()
cnx.close()

結果

(1, datetime.datetime(2016, 12, 12, 9, 59, 55), datetime.datetime(2016, 12, 12, 8, 0))
(2, datetime.datetime(2016, 12, 12, 10, 0), datetime.datetime(2016, 12, 12, 9, 0))
(3, datetime.datetime(2016, 12, 12, 10, 0, 5), datetime.datetime(2016, 12, 12, 9, 0))
(6, datetime.datetime(2016, 12, 12, 10, 0, 20), datetime.datetime(2016, 12, 12, 9, 0))
(4, datetime.datetime(2016, 12, 12, 11, 0, 10), datetime.datetime(2016, 12, 12, 10, 0))
(5, datetime.datetime(2016, 12, 12, 11, 0, 15), datetime.datetime(2016, 12, 12, 10, 0))