# Path: ldif/util/file_util.py
# def readlines(p):
# def readbin(p):
# def writebin(p, s):
# def writetxt(p, s):
# def write_np(path, arr):
# def read_grd(path):
# def read_sif_v1(path, verbose=False):
# def read_lines(p):
# def read_image(p):
# def read_npz(p):
# def read_np(p):
# def read_txt_to_np(p):
# def read_py2_pkl(p):
# def write_mesh(path, mesh):
# def read_mesh(path):
# def read_csv(path):
# def read_normals(path_to_dir, im_count=20, leading='_depth'):
# def write_points(path_ext_optional, points):
# def write_grd(path, volume, world2grid=None):
# def write_depth_image(path, depth_image):


import sys
import subprocess as sp
from ldif.util.file_util import log
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""Utilities for managing gpus."""





def get_free_gpu_memory(cuda_device_index):
  """Returns the current # of free megabytes for the specified device."""
  if sys.platform == "darwin":
    # No GPUs on darwin...
    return 0
  result = sp.check_output('nvidia-smi --query-gpu=memory.free '
                           '--format=csv,nounits,noheader',
                           shell=True)
  result = result.decode('utf-8').split('\n')[:-1]
  log.verbose(f'The system has {len(result)} gpu(s).')
  free_mem = int(result[cuda_device_index])

# Path: ldif/util/file_util.py
# def readlines(p):
# def readbin(p):
# def writebin(p, s):
# def writetxt(p, s):
# def write_np(path, arr):
# def read_grd(path):
# def read_sif_v1(path, verbose=False):
# def read_lines(p):
# def read_image(p):
# def read_npz(p):
# def read_np(p):
# def read_txt_to_np(p):
# def read_py2_pkl(p):
# def write_mesh(path, mesh):
# def read_mesh(path):
# def read_csv(path):
# def read_normals(path_to_dir, im_count=20, leading='_depth'):
# def write_points(path_ext_optional, points):
# def write_grd(path, volume, world2grid=None):
# def write_depth_image(path, depth_image):


# Path: ldif/util/gpu_util.py
import sys
import subprocess as sp
from ldif.util.file_util import log

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""Utilities for managing gpus."""





def get_free_gpu_memory(cuda_device_index):
  """Returns the current # of free megabytes for the specified device."""
  if sys.platform == "darwin":
    # No GPUs on darwin...
    return 0
  result = sp.check_output('nvidia-smi --query-gpu=memory.free '
                           '--format=csv,nounits,noheader',
                           shell=True)
  result = result.decode('utf-8').split('\n')[:-1]
  log.verbose(f'The system has {len(result)} gpu(s).')
  free_mem = int(result[cuda_device_index])

  log.info(f'The {cuda_device_index}-th GPU has {free_mem} MB free.')