





import fcntl
import os
import socket

import torch
import torch.distributed as dist







def    _lowerCamelCase  (*__lowerCamelCase  :     Dict     )   ->    Optional[Any]:
			with open(__lowerCamelCase					,  "r"     ) as fh:
						fcntl.flock(__lowerCamelCase					,  fcntl.LOCK_EX     )
						try:
									print(*__lowerCamelCase     )
						finally:
									fcntl.flock(__lowerCamelCase					,  fcntl.LOCK_UN     )


lowerCAmelCase_   :   int       =					int(os.environ["LOCAL_RANK"])
torch.cuda.set_device(local_rank)
lowerCAmelCase_   :   List[Any]       =					torch.device("cuda", local_rank)
lowerCAmelCase_   :   List[str]       =					socket.gethostname()

lowerCAmelCase_   :   str       =					f"""[{hostname}-{local_rank}]"""

try:
		# test distributed
		dist.init_process_group("nccl")
		dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM)
		dist.barrier()

		# test cuda is available and can allocate memory
		torch.cuda.is_available()
		torch.ones(1).cuda(local_rank)

		# global rank
		lowerCAmelCase_   :   Union[str, Any]       =					dist.get_rank()
		lowerCAmelCase_   :   Dict       =					dist.get_world_size()

		printflock(f"""{gpu} is OK (global rank: {rank}/{world_size})""")

		dist.barrier()
		if rank == 0:
				printflock(f"""pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}""")

except Exception:
		printflock(f"""{gpu} is broken""")
		raise










'''simple docstring'''


def    _lowerCamelCase  (__lowerCamelCase  :     int = 400_0000     )   ->    int:
			a__     = [0, 1]
			a__     = 0
			while fib[i] <= n:
						fib.append(fib[i] + fib[i + 1]     )
						if fib[i + 2] > n:
									break
						i += 1
			a__     = 0
			for j in range(len(__lowerCamelCase     ) - 1     ):
						if fib[j] % 2 == 0:
									total += fib[j]

			return total


if __name__ == "__main__":
		print(f"""{solution() = }""")



